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

feat(module): support server-specific runtime configuration #247

Merged
merged 3 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/pages/4.advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ title: Advanced
description: 'Real-life advanced usages of the strapi module.'
---

## Server-Specific Configuration

You can apply configuration based on whether a request is processed on the browser or server by using Nuxt [runtimeConfig](https://v3.nuxtjs.org/guide/features/runtime-config). Options provided directly over `runtimeConfig` field will override options provided in the `strapi` field of Nuxt configuration.

```ts
export default defineNuxtConfig({
// ...
runtimeConfig: {
strapi: { // nuxt/strapi options available server-side
url: 'http://example-strapi-instance:1337'
},
public: {
strapi: { // nuxt/strapi options available client-side
url: 'https://strapi.example.com'
}
}
},
// nuxt/strapi options available on both client and server
strapi: {
prefix: '/api'
}
// ...
})
```

## Async data

To take full advantage of server-side rendering, you can use Nuxt [useAsyncData](https://v3.nuxtjs.org/docs/usage/data-fetching#useasyncdata) composable:
Expand Down
7 changes: 7 additions & 0 deletions example/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export default defineNuxtConfig({
buildModules: [
module
],
// example of separate client/server URLs
/* runtimeConfig: {
strapi: { url: 'http://localhost:1337' },
public: {
strapi: { url: 'http://content:1337' }
}
}, */
strapi: {
url: 'http://localhost:1337'
}
Expand Down
4 changes: 3 additions & 1 deletion example/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<h1>@nuxtjs/strapi</h1>

<h2>Url</h2>
<pre>{{ url }}</pre>
<client-only>
<pre>{{ url }}</pre>
</client-only>
<h2>Version</h2>
<pre>{{ version }}</pre>

Expand Down
6 changes: 6 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export default defineNuxtModule<ModuleOptions>({
version: options.version,
cookie: options.cookie
})
nuxt.options.runtimeConfig.strapi = defu(nuxt.options.runtimeConfig.strapi, {
url: options.url,
prefix: options.prefix,
version: options.version,
cookie: options.cookie
})

// Transpile runtime
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapiGraphQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useStrapiClient } from './useStrapiClient'

export const useStrapiGraphQL = () => {
const client = useStrapiClient()
const config = useRuntimeConfig().public
const config = useRuntimeConfig()

return <T> (query: string): Promise<T> => {
return client('/graphql', { method: 'POST', body: { query }, baseURL: config.strapi.url })
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapiToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCookie, useNuxtApp, useRuntimeConfig } from '#app'

export const useStrapiToken = () => {
const nuxtApp = useNuxtApp()
const config = useRuntimeConfig().public
const config = useRuntimeConfig()

nuxtApp._cookies = nuxtApp._cookies || {}
if (nuxtApp._cookies.strapi_jwt) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapiUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRuntimeConfig } from '#app'

export const useStrapiUrl = (): string => {
const config = useRuntimeConfig().public
const config = useRuntimeConfig()
const version = config.strapi.version
return version === 'v3' ? config.strapi.url : `${config.strapi.url}${config.strapi.prefix}`
}
2 changes: 1 addition & 1 deletion src/runtime/composables/useStrapiVersion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRuntimeConfig } from '#app'

export const useStrapiVersion = (): string => {
const config = useRuntimeConfig().public
const config = useRuntimeConfig()
return config.strapi.version
}