-
Notifications
You must be signed in to change notification settings - Fork 172
Fix Inertia SSR errors #53
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
Changes from 6 commits
bddd3f6
bb24863
3e7b127
4e292f7
011ee04
689d780
4951bcd
0039931
51059af
cf70d5e
b11cf1e
09dad2e
c1a0fcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,11 @@ const sidebarNavItems: NavItem[] = [ | |
]; | ||
|
||
export default function SettingsLayout({ children }: PropsWithChildren) { | ||
// For SSR, we can't access the window location, so we only render the layout on the client | ||
if (typeof window === 'undefined') { | ||
return null; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is url in usePage hooks, why not just use that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We certainly could, both will accomplish the same thing I'm pretty sure. This is just a preference of being explicit about rendering on the server. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess in that context I prefer that way too, thanks for the answer. |
||
|
||
const currentPath = window.location.pathname; | ||
|
||
return ( | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { createInertiaApp } from '@inertiajs/react'; | ||
import createServer from '@inertiajs/react/server'; | ||
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
import { type RouteName, route } from 'ziggy-js'; | ||
|
||
const appName = import.meta.env.VITE_APP_NAME || 'Laravel'; | ||
|
||
createServer((page) => | ||
createInertiaApp({ | ||
page, | ||
render: ReactDOMServer.renderToString, | ||
title: (title) => `${title} - ${appName}`, | ||
resolve: (name) => resolvePageComponent(`./pages/${name}.tsx`, import.meta.glob('./pages/**/*.tsx')), | ||
setup: ({ App, props }) => { | ||
/* eslint-disable */ | ||
// @ts-expect-error | ||
global.route<RouteName> = (name, params, absolute) => | ||
route(name, params as any, absolute, { | ||
// @ts-expect-error | ||
...page.props.ziggy, | ||
// @ts-expect-error | ||
location: new URL(page.props.ziggy.location), | ||
}); | ||
/* eslint-enable */ | ||
|
||
return <App {...props} />; | ||
}, | ||
}), | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { route as routeFn } from "ziggy-js"; | ||
|
||
declare global { | ||
const route: typeof routeFn; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,5 +114,9 @@ | |
}, | ||
"jsx": "react-jsx" | ||
}, | ||
"include": ["resources/js/**/*.ts", "resources/js/**/*.tsx"] | ||
"include": [ | ||
"resources/js/**/*.ts", | ||
"resources/js/**/*.d.ts", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would "resources/js/**/*.d.ts" not already be targeted with "resources/js/**/*.ts"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it is necessary, but I feel like it's not a bad thing to include because it helps with clarity. Some devs perfer to explicitly include the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RV7PR, correct. Technically not necessary, but I like to include them to be very explicit that the project include type declaration files. Similar to how the original Breeze templates worked as well. |
||
"resources/js/**/*.tsx", | ||
] | ||
} |
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.
@JoeyMckenzie This should run
npm run build:ssr
instead ofnpm run build
. Go ahead and get that updated and we'll merge this in.Really appreciate the help!
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.
Good catch, updated.