GithubHelp home page GithubHelp logo

Comments (5)

binoy14 avatar binoy14 commented on May 23, 2024 1

@max888 how are you including Sanity component inside nextjs, from the code provided that shouldn't have any issues. Possible that the imports are incorrect in some place. If you can provide example recreating the issue it would help in debugging further.

from sanity.

MohitGupta14 avatar MohitGupta14 commented on May 23, 2024

I just clone it, it was working fine with pnpm.

from sanity.

max888 avatar max888 commented on May 23, 2024

Hi @binoy14 I have a 'sanity' folder in the root of my nextjs application (outside of the app folder).

Then I have a file located in @/app/lib/sanity with the following code to connect to the client:

import { createClient } from 'next-sanity'
import imageUrlBuilder from '@sanity/image-url'

export const client = createClient({
  projectId: "xxxxxxx", // replaced with my real id
  dataset: "production",
  apiVersion: "2024-01-01",
  // unless you have caching for your front end, `useCdn` should be `true` for most production environments
  useCdn: true, 
})

const builder = imageUrlBuilder(client)

export function urlFor(source) {
  return builder.image(source)
}

Then throughout the nextjs app I import the sanity client or image builder in my components using:

import { urlFor } from "@/app/lib/sanity.js";
import { client } from "@/app/lib/sanity.js";

The weird thing is that if I extract/copy this 'sanity' folder somewhere outside of the nextjs application and run it using npm run dev, it works fine. It's only when I try and run it whilst inside the nextjs app that I get the issue. However, in the 'sanity' folder I'm not making any references to code in the nextjs app. I'm just defining schemas. That's why I'm not sure how there is an error there when trying to run it.

I will try and make an example recreating the issue to help debug.

from sanity.

mariuslundgard avatar mariuslundgard commented on May 23, 2024

Do you have a "baseUrl" configuration in your tsconfig.json?

from sanity.

max888 avatar max888 commented on May 23, 2024

Hi @mariuslundgard thanks for following up. I am not using typescript for my project but in my jsconfig.json I have have a `"baseUrl" set like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./*"]
    }
  }
}

I 'fixed' the issue by moving the sanity part into a new sub folder within the project and it worked. Seemed to me like it could have been a weird npm caching issue. Although I had tried npm cache clean and a few other things with no luck.

My nextjs app folder structure was:

/app
    -layout.js
    - page.js
    - etc...
/sanity
    /schemaTypes
    -sanity.cli.js
    -sanity.config.js
    -package.json
    -etc...
jsconfig.json
next-config.mjs
package.json
...etc

I then moved the /sanity folder to a different subfolder at /new/sanity , ran npm run dev and it worked.

from sanity.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.