Skip to content

Commit 685e0b3

Browse files
samydoesitatinux
andauthored
feat: opt out of session plugin with loadStrategy: 'none'(#472)
Co-authored-by: Sébastien Chopin <atinux@gmail.com> Co-authored-by: Sébastien Chopin <seb@nuxt.com>
1 parent b030b7d commit 685e0b3

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,20 @@ export default defineNuxtConfig({
609609

610610
When using the `client-only` load strategy, the user session can still be manually fetched on the server side by calling `fetch` from the `useUserSession` composable.
611611

612+
### Disable session loading
613+
614+
You may also choose to disable session loading entirely, with the `loadStrategy` option in your `nuxt.config.ts`:
615+
616+
```ts
617+
export default defineNuxtConfig({
618+
auth: {
619+
loadStrategy: 'none'
620+
}
621+
})
622+
```
623+
624+
When using the `none` load strategy, the user session can still be manually fetched by calling `useUserSession().fetch()`.
625+
612626
### `<AuthState>` component
613627

614628
You can use the `<AuthState>` component to safely display auth-related data in your components without worrying about the rendering mode.

playground/nuxt.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export default defineNuxtConfig({
2929
auth: {
3030
webAuthn: true,
3131
atproto: true,
32-
// loadStrategy: 'client-only'
32+
// loadStrategy: 'none',
33+
// loadStrategy: 'client-only',
3334
},
3435
icon: {
3536
customCollections: [{

src/module.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface ModuleOptions {
4343
* Session load strategy
4444
* @default 'server-first'
4545
*/
46-
loadStrategy?: 'server-first' | 'client-only'
46+
loadStrategy?: 'server-first' | 'client-only' | 'none'
4747
}
4848

4949
declare module 'nuxt/schema' {
@@ -59,7 +59,7 @@ declare module 'nuxt/schema' {
5959

6060
interface PublicRuntimeConfig {
6161
auth: {
62-
loadStrategy: 'server-first' | 'client-only'
62+
loadStrategy: 'server-first' | 'client-only' | 'none'
6363
}
6464
}
6565
}
@@ -96,8 +96,10 @@ export default defineNuxtModule<ModuleOptions>({
9696
// App
9797
addComponentsDir({ path: resolver.resolve('./runtime/app/components') })
9898
addImports(composables)
99-
addPlugin(resolver.resolve('./runtime/app/plugins/session.server'))
100-
addPlugin(resolver.resolve('./runtime/app/plugins/session.client'))
99+
if (options.loadStrategy !== 'none') {
100+
addPlugin(resolver.resolve('./runtime/app/plugins/session.server'))
101+
addPlugin(resolver.resolve('./runtime/app/plugins/session.client'))
102+
}
101103
// Server
102104
addServerPlugin(resolver.resolve('./runtime/server/plugins/oauth'))
103105
addServerImportsDir(resolver.resolve('./runtime/server/lib/oauth'))

0 commit comments

Comments
 (0)