3

I would like to make specific link from my page ( https://www.bizraport.pl/organizacje ) prerendered. My issue is that page uses (in both +page.server.js and +page.svelte) url.searchParams.get which makes an error:

Error: Cannot access url.searchParams on a page with prerendering enabled

I do get that there is infinite number of urls which can be created with parameters. However in my +page.server.js there is added export const prerender = 'auto'; and in svelte.config.js:

prerender: {
  entries: ["/organizacje"],
},

So why isn't it intelligent enough, to just get page /organizacje and do not worry about any url parameters. If there is any it should be standard approach (without prerender)

Links about similar topics:

https://dev.to/marcdaframe/sveltekit-dynamic-routes-static-renderer-17dl

How to use get parameter on sveltekit when use adapter-static?

Tried to make prerenderer work with specific url. This url is based on parameters, however not the main one. It may be filtered by additional parameters.

3
  • How makes accessing the searchParams in +page.server.js sense, if the page is pre-rendered? Are you trying to update the potentially stale data of the pre-rendered version? Commented Jul 10, 2023 at 14:22
  • Page "bizraport.pl/organizacje" should be pre-rendered. And only this page. But for example: bizraport.pl/organizacje?page=2 or bizraport.pl/organizacje?kierunek_sortowania=true Or anything else with any other parameter should ask server (+page.server.js) to provide proper information Commented Jul 11, 2023 at 13:52
  • I'm having the same trying to reach the same thing and I simply can't. Commented Nov 10, 2023 at 22:47

3 Answers 3

6
+100

Here is a comment from Rich explaining why search parameters cannot be used during the pre-rendering:

When you prerender, you're writing files to the filesystem so that a simple webserver can serve them. Query parameters can't exist on the filesystem (both in the literal sense that ? isn't an allowed character on all filesystems, and in the sense that the webserver will ignore them), so if you have a prerendered endpoint called /api/posts/get then it doesn't matter how many times you call it with different parameters, it can only be saved as a file once. The files need to have different paths (/api/posts/[slug]), or it won't work.

A way around this is to use beforeUpdate:

<script>
  let myParameter = null;
  beforeUpdate(() => {
    myParameter =  url.searchParams.get('my-parameter');
  });
</script>

<div>
  <!-- Use `myParameter` -->
</div>
Sign up to request clarification or add additional context in comments.

Comments

4

Accessing url.searchParams during prerendering is forbidden - on the server. As written in the documentation, you can get around this by retriving the parameters in the client, using onMount():

<script>
import { onMount } from 'svelte';

let page;

onMount(() => {
  params = url.searchParams.get('page');
})
</script>

Comments

2

If you run into this error for adapter-static / SSG, you can detect the build process via building and avoid accessing page.url.searchParams while building, Svelte v5 and SvelteKit v2:

<script>
import { building } from '$app/environment';

let results = $derived.by(() => {
    const searchText = !building ? page.url.searchParams.get('searchText') || "" : "";

Thanks very much to Scott (Svelte Discord) and Pablopang.svelte.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.