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

[1.x] Return early when using router.on() during SSR #1715

Merged
merged 4 commits into from
Sep 24, 2024

Conversation

RobertBoes
Copy link
Contributor

@RobertBoes RobertBoes commented Oct 31, 2023

When using router.on() it is always executed, this means the following won't work during SSR:

<script setup>
import { router } from '@inertiajs/vue3'

router.on('start', (event) => {
  console.log(`Starting a visit to ${event.detail.visit.url}`)
})
</script>

Instead, we'd need to wrap this in a onMounted hook, or check if the document/window exists;

<script setup>
import { router } from '@inertiajs/vue3'
import { onMounted } from 'vue'

onMounted(() => {
  router.on('start', (event) => {
    console.log(`Starting a visit to ${event.detail.visit.url}`)
  })
})
</script>

But since router.on() is already just a wrapper for document.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

jrson83 added a commit to inertiajs-revamped/inertia that referenced this pull request Apr 26, 2024
Copy link
Collaborator

@pedroborges pedroborges left a 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)

packages/core/src/router.ts Outdated Show resolved Hide resolved
@pedroborges pedroborges changed the title Return early when using router.on() during SSR [1.x] Return early when using router.on() during SSR Sep 24, 2024
@pedroborges pedroborges added the core Related to the core Inertia library label Sep 24, 2024
Copy link
Contributor

@joetannenbaum joetannenbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@pedroborges
Copy link
Collaborator

Thanks, @RobertBoes!

@pedroborges pedroborges merged commit b4bbd75 into inertiajs:master Sep 24, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to the core Inertia library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants