Skip to content

Commit

Permalink
fix: fetch session directly when ssr disabled (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux authored Aug 28, 2024
1 parent cf75ab1 commit be2ca7e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default defineNuxtConfig({
// ssr: false,
compatibilityDate: '2024-06-17',
devServer: {
host: '127.0.0.1',
Expand Down
8 changes: 8 additions & 0 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
...
</template>
</AuthState>
<UButton
to="/secret"
class="mt-2"
variant="link"
:padded="false"
>
Secret page
</UButton>
</UPageBody>
</UPage>
</template>
19 changes: 19 additions & 0 deletions playground/pages/secret.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script setup>
definePageMeta({
middleware() {
const { loggedIn } = useUserSession()
if (!loggedIn.value) {
return navigateTo('/')
}
},
})
</script>

<template>
<div>
<h1>Secret</h1>
<NuxtLink to="/">
Home
</NuxtLink>
</div>
</template>
6 changes: 5 additions & 1 deletion src/runtime/app/plugins/session.client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { defineNuxtPlugin, useUserSession } from '#imports'

export default defineNuxtPlugin(async (nuxtApp) => {
if (!nuxtApp.payload.serverRendered || Boolean(nuxtApp.payload.prerenderedAt) || Boolean(nuxtApp.payload.isCached)) {
if (!nuxtApp.payload.serverRendered) {
await useUserSession().fetch()
}
else if (Boolean(nuxtApp.payload.prerenderedAt) || Boolean(nuxtApp.payload.isCached)) {
// To avoid hydration mismatch
nuxtApp.hook('app:mounted', async () => {
await useUserSession().fetch()
})
Expand Down

0 comments on commit be2ca7e

Please sign in to comment.