-
-
Notifications
You must be signed in to change notification settings - Fork 446
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
[1.x] Return early when using router.on()
during SSR
#1715
Conversation
… [inertia/pull/1715](inertiajs/inertia#1715))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested the proposed fix locally, and it works as expected. It aligns with other methods that return early in SSR mode.
Previously, using router.on()
during SSR caused the following error:
[Vue warn]: Unhandled error during execution of setup function
at <Layout errors= {} appName="Vue 3 Playground" >
ReferenceError: document is not defined
at F.on (file:///Users/pedro/Code/open-source/inertia/playgrounds/vue3/bootstrap/ssr/ssr.mjs:301:5)
at setup (file:///Users/pedro/Code/open-source/inertia/playgrounds/vue3/bootstrap/ssr/ssr.mjs:648:8)
at _sfc_main$6.setup (file:///Users/pedro/Code/open-source/inertia/playgrounds/vue3/bootstrap/ssr/ssr.mjs:741:25)
at callWithErrorHandling (/Users/pedro/Code/open-source/inertia/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:192:19)
at setupStatefulComponent (/Users/pedro/Code/open-source/inertia/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7551:25)
at setupComponent (/Users/pedro/Code/open-source/inertia/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7512:36)
at renderComponentVNode (/Users/pedro/Code/open-source/inertia/playgrounds/vue3/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:620:15)
at renderVNode (/Users/pedro/Code/open-source/inertia/playgrounds/vue3/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:752:14)
at renderComponentSubTree (/Users/pedro/Code/open-source/inertia/playgrounds/vue3/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:707:7)
at renderComponentVNode (/Users/pedro/Code/open-source/inertia/playgrounds/vue3/node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:638:12)
router.on()
during SSRrouter.on()
during SSR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
Thanks, @RobertBoes! |
When using
router.on()
it is always executed, this means the following won't work during SSR:Instead, we'd need to wrap this in a
onMounted
hook, or check if the document/window exists;But since
router.on()
is already just a wrapper fordocument.addEventListener()
I'd expect the wrapper to take care of that for you.A simple fix I've made here is to simply check if we're in ssr with the
isServer
constant, if it is we return an empty void function