Skip to content

fix: construct query params from location.search on rehydrate #1511

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

Closed
wants to merge 2 commits into from

Conversation

pzerelles
Copy link
Contributor

@pzerelles pzerelles commented May 20, 2021

Fixes #669

When rehydrating a page, the query params were set to URLSearchParams(s(page.query.toString())). This change uses location.search when page.query is empty.

@pzerelles pzerelles changed the title fix: construct query params from location.search on rehydrate (#1218) fix: construct query params from location.search on rehydrate May 20, 2021
@benmccann
Copy link
Member

This isn't a section of code I'm super familiar with. Can you explain under what scenarios page.query.toString() would be different from location.search?

@benmccann benmccann added the bug Something isn't working label May 20, 2021
@pzerelles
Copy link
Contributor Author

pzerelles commented May 20, 2021

Actually I just didn't want to prevent what was happening before if page.query is set. We could probably just use location.search. Maybe the original author can tell us if it is really necessary.

@benmccann
Copy link
Member

Yes, this seems simpler. Still, I'm wondering what exactly the bug was. E.g. there are other places we use page and I wouldn't want there to be issues there as well. Can you explain why or when page.query.toString() is different from location.search?

@pzerelles
Copy link
Contributor Author

The bug was that when rehydrating pages on static sites, the query string from the url was not used, only the query prop of the page object which was empty.

@falsetru

This comment has been minimized.

@benmccann
Copy link
Member

benmccann commented May 26, 2021

the query prop of the page object which was empty

That's what concerns me about this fix. It seems like a bit of a workaround. Why is the query prop empty? It seems like the query prop is still going to be broken for anyone who tries to use it

This is only going to work on the browser-side on rehydration, so it seems like it could catch people off-guard that the server-side isn't rendered as they expect

@benmccann
Copy link
Member

I added an explanation for what's happening on the ticket: #669 (comment). I'm not sure what the other maintainers think, but at the moment I'd lean towards disallowing usage of $page.query on prerendered pages instead

@johnnysprinkles

This comment has been minimized.

@johnnysprinkles
Copy link
Contributor

johnnysprinkles commented May 29, 2021

I'd be curious if @benmccann or @pz-mxu has anything thoughts about this RFC I submittted today, making the argument that this PR is a special case of a general issue. sveltejs/rfcs#52

@Rich-Harris
Copy link
Member

Using page.query is deliberate — the idea is to guarantee consistency between server and client renders. Breaking that guarantee feels like a dangerous road to go down. If you don't know location.search at SSR time (because you're prerendering) then it feels more appropriate to disable SSR for that page than to render an incorrect page, surely?

@benmccann
Copy link
Member

Thanks for taking the time to investigate and propose this fix. I'm going to go ahead and close this given that Rich has the same opinion as I do that this isn't the correct way to address the issue

@johnnysprinkles
Copy link
Contributor

So this issue seems to be fixed now... if I serve a staticly built page and put this code in there:

	if (!prerendering) {
		console.log($page.url.searchParams.get('foo'));
	}

I get the actual runtime per-request value of "foo" as seen by the client, instead of the value as was snapshotted at prerender time. I'd like to find out which commit change all this around!

@johnnysprinkles johnnysprinkles mentioned this pull request May 15, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

$page.query.get(..) returns null for prerendered pages
5 participants