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

Return early when using router.on() during SSR #1715

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant