paoloricciuti / sveltekit-search-params

The easiest way to read and WRITE from query parameters in sveltekit.
https://sveltekit-search-params.netlify.app
MIT License
497 stars 15 forks source link

Possible bug: Inconsistent behavior with Node 20 #26

Open devinellis opened 1 year ago

devinellis commented 1 year ago

Describe the bug

First off, I'll say that I haven't verified if this happens without the use of your plugin.

const search = queryParam('q', ssp.string(), {pushHistory: false})
...
<input type="search" name="search" placeholder="Search" bind:value={$search} />

Running this on Node 18, no calls to data.json happen when typing in the input. But when I upgrade to Node 20, I see a request to data.json on every keystroke.

It took me a looong time to figure out what the culprit was. I created a whole Stackblitz for you, only to find that the issue wasn't there because apparently it runs Node 16.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-d1rzdh?file=README.md

Logs

No response

stackblitz[bot] commented 1 year ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

paoloricciuti commented 1 year ago

Mmm in the stackblitz you sent is completely normal that no request is made...are you saying that if you upgrade to node 20 the load function will run on every keystroke? That seems like a sveltekit bug more than a sveltekit-search-params bug but i'll take a look...in the meantime please can you confirm this?

devinellis commented 1 year ago

Yes that's correct, sorry for the confusion. I couldn't find a way to get stackblitz to use node 20.

For example, the project im working on had been on Node 18 and it was fine. But at some point my docker image updated to Node 20 and that's when the issue began. Forcing docker to node 18 fixes the problem.

paoloricciuti commented 1 year ago

I can confirm this is actually a bug with sveltekit...do you prefer to open this issue on the kit repository? Otherwise i can do it for you.

devinellis commented 1 year ago

If you don’t mind maybe you can, if you have clean repro steps. It seems more important coming from a plugin maintainer such as yourself.

paoloricciuti commented 1 year ago

Sure i'll prepare a repro and open an issue on kit...thanks for reporting

paoloricciuti commented 1 year ago

Uh i can't reproduce anymore even with node 20 and your code

paoloricciuti commented 1 year ago

https://gitpod.io#snapshot/f7d1217a-72f1-4df1-9f50-828d78c19639

devinellis commented 1 year ago

If I run $ node -v in the terminal on that gitpod, it says Node v18. I can't figure out where that's being configured from... I've never used gitpod before.

paoloricciuti commented 1 year ago

If I run $ node -v in the terminal on that gitpod, it says Node v18. I can't figure out where that's being configured from... I've never used gitpod before.

You can just nvm install 20 in the terminal and than run the app

devinellis commented 1 year ago

OK I figured out how to repro. You need to check the URL in page.server.ts and it somehow becomes aware of that. Change the handler to something like this:

export const load = ({url}) => {
  const now = new Date().toString();
  if(url.searchParams.has('a')) {
    console.log('noop');
  }
  console.log(now);
  return { now };
};

I can't get it to not happen, even switching back to node 18 in gitpod, downgrading sveltekit, etc. Its weird that this doesn't happen when I use the docker image 18-alpine, because the server handler is the exact same.

paoloricciuti commented 1 year ago

OK I figured out how to repro. You need to check the URL in page.server.ts and it somehow becomes aware of that. Change the handler to something like this:

export const load = ({url}) => {
  const now = new Date().toString();
  if(url.searchParams.has('a')) {
    console.log('noop');
  }
  console.log(now);
  return { now };
};

I can't get it to not happen, even switching back to node 18 in gitpod, downgrading sveltekit, etc. Its weird that this doesn't happen when I use the docker image 18-alpine, because the server handler is the exact same.

If you do this than it's the actual expected behavior of sveltekit...if you use searchParams inside the load function it will invalidate and rerun when the search params change