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

Allow page.query to be accessed in load function even when prerender is true #2262

Closed
mattjennings opened this issue Aug 21, 2021 · 2 comments

Comments

@mattjennings
Copy link

Describe the problem

#2104 introduced throwing an error when page.query was accessed. This makes sense to prevent usage on the client, but now you cannot use it during load which was beneficial for prerendering crawled pages. For example, a page displaying a list of blog posts with pagination using a ?page=X query. As long as those can be crawled, using query is OK here.

Describe the proposed solution

Check if page.query is being accessed on the server before throwing the error

https://github.com/sveltejs/kit/pull/2104/files#diff-7266c49c71803f38dda70345d857f13a7a2494cc649b70af6b689627199f5b79R53-R63

Alternatives considered

I could use params instead, which is probably better in most scenarios, but in my case I'm using it on my index page and it's more awkward than using a query. I would prefer mywebsite.com/?page=2 over mywebsite.com/2.

Importance

would make my life easier

Additional Information

Here's an example of a template I made that utilized crawled pages with queries (you'll have to set const PAGE_SIZE = 1 in routes/index.svelte to get more than 1 page):

https://github.com/mattjennings/sveltekit-blog-template

The build will work with kit 1.0.0-next.144, but break on 145

@benmccann
Copy link
Member

Even on 144 your template was broken in some ways - it was just hidden to you. E.g. if you build the site and visit /?page=2 directly it will still load page 1. That's not what I'd expect as a user. This check pointed out that what you're doing is not valid

Prerendering works by saving a static file with the name of the page. In your case index.html. But we can't save an index.html and index.html?page=2. ? is an invalid character on at least some file systems and your web server wouldn't know to serve a different file based on the query string.

If you really want to use ?page=2 then be aware that it's something that only works on the client-side and use location in onMount instead.

@mattjennings
Copy link
Author

I hadn't realized it only worked on client-side navigation (and obviously that's not what I'd expect as a user either). I appreciate the explanation, as I wasn't sure how it "worked" in the first place, but that makes sense.

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

2 participants