-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
$page.query.get(..) returns null for prerendered pages #669
Comments
I'm having the same problem 🖐️ @falsetru Your solution should only work within the browser because |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@andreasnuesslein I use To avoid accessing document outside browser, you can use import { browser } from '$app/env';
...
const qs = browser ? document.location.search : '';
const query = new URLSearchParams(qs);
const name = query.get('name') || 'unknown'; |
yes.. if you use this solution, you should turn off [ssr: false] in svelte.config.cjs. |
This isn't just |
The issue here is that a prerendered page is a page that has already been rendered and saved as HTML. Since it's been rendered, there's no way on the server-side to use the query. You'd have to disable prerendering if you want to use the query on the server. Right now, it's pretty easy not to realize that there's an issue there. There are a few options:
update: I think Rich's thinking largely aligns with my proposed solution given his comment here: #1511 (comment) |
I run this code after build: import replace from 'replace'
replace({
regex: 'query: new URLSearchParams(.+),',
replacement: 'query: new URLSearchParams(location.search),',
paths: ['build'],
recursive: true
}) So, the problem gone~ |
When the page is already hydrated, further navigation will update the $page store with the query params. Not having them available after hydration makes the $page store's query prop unusable for static sites, because navigation will lead to other results than reloads of the same url. Of course one should be aware of that a prerendered page does not have query params for search engines. Possible additional hydration modes could update the $page store on hydration with the query params. The use of query params is of course not always the best way to handle things and it will never be optimal in all situations. Developer should know the implications, especially with prerendered pages. Giving warnings in the documentation with a couple of use cases could help there, too, without taking away the simplicity of having them available in the $page store. |
@mzaini30 You probably saw my RFC sveltejs/rfcs#52 that I mentioned in the PR related to this issue. I did some exploration into web server interpolation and the last line of this commit has a html = f.read().decode("utf-8")
return html.replace("[id]\"", match[1] + "\"").encode("utf-8") Of course just updating SvelteKit itself to do this would be the ideal route. |
@johnnysprinkles So, can you generate |
This could be implemented by making
|
Describe the bug
$page.query.get(...)
returnsnull
.To Reproduce
Expected behavior
abc
printed, but gotnull
.Information about your SvelteKit Installation:
Diagnostics
npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers
Your browser
Google Chrome Version 89.0.4389.114 (Official Build) (64-bit)
Your adapter
@sveltejs/adapter-static
Additional context
new URLSearchParams(document.location.search).get('name')
.kit/packages/kit/src/runtime/server/page.js
Line 398 in 4a1c04a
The text was updated successfully, but these errors were encountered: