question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Describe the bug $page.query.get(...) returns null.

To Reproduce

git clone --branch issue-1 https://github.com/falsetru/sveltekit-test.git issue-1
cd issue-1
npm install
npm run build
npx sirv build/  # `npm run start` will hide the issue.

Expected behavior abc printed, but got null.

Information about your SvelteKit Installation:

Diagnostics
  • The output of npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers
  System:
    OS: Linux 5.8 Ubuntu 20.10 (Groovy Gorilla)
    CPU: (4) x64 Intel(R) Core(TM) i7-7600U CPU @ 2.80GHz
    Memory: 763.02 MB / 15.39 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 12.18.2 - /usr/bin/node
    npm: 7.7.6 - ~/.npm-packages/bin/npm
  Browsers:
    Chrome: 89.0.4389.114
    Firefox: 87.0
  npmPackages:
    @sveltejs/kit: next => 1.0.0-next.67 
    svelte: ^3.29.0 => 3.37.0 
    vite: ^2.1.0 => 2.1.5 

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
benmccanncommented, May 29, 2021

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:

  • prerendering could consider the URL as part of the page lookup key. This seems difficult though because users could potentially type any arbitrary query string and what happens if they type one that hasn’t been prerendered
  • probably what I’d prefer is to have query be null or throw an exception if you try to use it during prerendering to make it more obvious that it’s something you shouldn’t do. If there’s something you want to do with the query string you should do it using the location either in onMount or behind an if (browser) check
  • as https://github.com/sveltejs/kit/pull/1511 suggests we could just ignore the inability to render on the server-side and then fix up the content during hydration. This option makes me a bit nervous. While it would work and might be easier for the user, I think it also hides the fact that your content might not be what you expect when a search engine sees it. It also would not be compatible with possible additional hydration modes that could be introduced in the future. E.g. if you consider the way that React does hydration where the DOM content is not updated, but event listeners are simply added, then this solution would not work

update: I think Rich’s thinking largely aligns with my proposed solution given his comment here: https://github.com/sveltejs/kit/pull/1511#issuecomment-850907659

2reactions
benmccanncommented, May 26, 2021

@benmccann Your solution should only work within the browser because document is not defined outside the browser (i.e. when adapter-static is prerendering everything)

I have no idea what you’re referring to as being my solution because I have not commented on this thread at all

Read more comments on GitHub >

github_iconTop Results From Across the Web

$page.query returns empty on prerendered pages · Issue #1218 ...
Describe the bug Using $page.query sometimes returns as empty for pages with a query string. To Reproduce In my experience, any basic page...
Read more >
Next.js router is returning query parameters as undefined on ...
It seems that Next.js initially gets the query object from window.__NEXT_DATA__ , which is the prerendered data from the server. And if you...
Read more >
General tips and best practices - Troubleshooting - Prerender.io
Prerender gives back raw or empty content. If the prerender returns an X-Prerender-Raw-Data header, the crawler cannot render the page and ...
Read more >
Next JS Pre-Rendering Pages - Page Content - YouTube
in this video we will be going over how to pre render a page that has a section that relies on external data.If...
Read more >
Client-Side Routing In Next.js - Smashing Magazine
Each page is a default exported React component from the pages ... about rendering in Next.js is that each page is pre-rendered in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found