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

How to supply url defaults when using SSR? #24

Open
ragulka opened this issue Jul 31, 2024 · 0 comments
Open

How to supply url defaults when using SSR? #24

ragulka opened this issue Jul 31, 2024 · 0 comments

Comments

@ragulka
Copy link

ragulka commented Jul 31, 2024

I have defined a few default bindings for my app routes, which depend on the currently logged in user.

How can I supply them to trail when using SSR? For the regular app, I have set up a watcher like the one below, but it does not seem to have any effect on SSR, as I get errors like Error: Ziggy error: 'user' parameter is required for route 'user.profile'

import { defineRoutes, store } from 'momentum-trail'
import { RouteCollection } from 'momentum-trail/dist/types/types'

declare global {
    interface Window {
        trail: RouteCollection
    }
}

/**
 * This composable is used by page layout components to watch for changes in URL defaults, which
 * are provided by the HandleInertiaRequests middleware. This allows switching app context from
 * one company to another, or from guest to authenticated user without a full page reload and
 * ensures Trail/Ziggy has the correct URL defaults available at all times.
 */
export const useUrlDefaultsWatcher = () => {
    watch(
        () => usePage().props.urlDefaults,
        (defaults: object) => {
            if (typeof window !== 'undefined' && window.trail) {
                window.trail.defaults = defaults
            } else {
                defineRoutes(Object.assign(store.getRoutes(), defaults))
            }
        },
        { immediate: true },
    )
}
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

No branches or pull requests

1 participant