Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for using the configured Sanity client in nuxt.config.js #3

Closed
mornir opened this issue Aug 23, 2020 · 6 comments
Closed

Support for using the configured Sanity client in nuxt.config.js #3

mornir opened this issue Aug 23, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@mornir
Copy link
Contributor

mornir commented Aug 23, 2020

When generating dynamic routes, I need the Sanity client to fetch all routes. Is there a way I can access the configured client in nuxt.config.js? Or do I need to import it and configure it again?

@mornir mornir added the question Further information is requested label Aug 23, 2020
@danielroe danielroe changed the title How to use the configured Sanity client in nuxt.config.js? Support for using the configured Sanity client in nuxt.config.js Aug 28, 2020
@danielroe danielroe added enhancement New feature or request and removed question Further information is requested labels Aug 28, 2020
@danielroe
Copy link
Collaborator

danielroe commented Aug 28, 2020

This is an interesting idea. Would importing a createClient helper that returns an auto configured client (based on the settings in nuxt.config) suit your use-case?

Proposed use:

import { createClient } from '@nuxtjs/sanity'

const client = createClient()

export default {
  // ...
}

@mornir
Copy link
Contributor Author

mornir commented Aug 30, 2020

Yes, that would perfect for my use case 👍

@mornir
Copy link
Contributor Author

mornir commented Sep 19, 2020

I had a look at the code. Actually there's already a helper function exported. So I can do this:

import { createClient } from '@nuxtjs/sanity'
export const configSanity = {
  projectId: 'xxxxxxx',
  withCredentials: true,
  useCdn: false,
  minimal: true,
}
const client = createClient(configSanity)
export default { 
 // nuxt config....
   sanity: {
      ...configSanity,
    },
}

I see that only the main nuxtModule function has access to the moduleOptions. So I guess it's not that easy to export an auto-configured client? If I want to submit a contribution with this enhancement, where should I start in the code?

@arlenpeiffer
Copy link

+1 to this idea

@danielroe
Copy link
Collaborator

The createClient function will create a minimal client. The challenge is that a lot of the configuration of the client comes from the nuxt.config file and so using it within the file runs the risk of circular dependency. If you're happy to redefine the configuration it should be possible to do:

// for minimal client
import { createClient } from '@nuxtjs/sanity'
// for official Sanity client
import createClient from '@sanity/client'

const client = createClient({
  projectId: 'xxxxxxx',
  withCredentials: true,
  useCdn: false,
})

// ... use client

Do you think this is straightforward enough? It doesn't feel that inconvenient to me, but I'm open to being persuaded otherwise.

@mornir
Copy link
Contributor Author

mornir commented Oct 11, 2020

Yes, this is what I'm doing and it's perfectly fine 😉 I'll submit a pull request to document it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants