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

Fix Deploys: Move all Logic from load to onMount #80

Merged
merged 9 commits into from
Jan 20, 2024
Merged

Conversation

coreyja
Copy link
Member

@coreyja coreyja commented Jan 20, 2024

Builds are currently failing with this error message

Accessing url.searchParams during prerendering is forbidden. If you need to use it, ensure you are only doing so in the browser (for example in onMount).

It was introduced in #79

This error is because we are trying to access url.searchParams in the load function and ALSO have preloading enabled.
Since load gets called on the server to pre-render, it doesn't allow us to access url.searchParams since that can only be known in the browser

But this must be new behavior, since it was working for us previously. I think it was working previously is because load was acting as a 'universal` loader and running both in the 'server' when preloading, and also in the client when mounting for the first time.

https://kit.svelte.dev/docs/load#universal-vs-server-when-does-which-load-function-run

I imagine the server load was getting an empty searchSet, but continuing to work. And then the client load correctly filled in the query param values

But from reading the Changelog I can't find anything that seems obviously like it would have broken this behavior

I do think this is a good change to prepare for v2, since it seems required there as well!

@coreyja
Copy link
Member Author

coreyja commented Jan 20, 2024

Hmm ya there are some issues with this approach

When I open Settings and then come back I get the error about needing a game even though there is one in the URL

Which kinda makes sense if onMount has already ran cause we already mounted the app

Edit: I think this is fixed in my latest commit, by making the back to game button use e.preventDefault() to use the history.back() instead of actually clicking the link

steps:
- uses: actions/checkout@v3
- run: npm ci
- run: npm run build
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this so that we know if future PRs are going to break builds, even if others tests pass

Comment on lines +58 to +67
const url = new URL(window.location.href);
settings = loadSettingsWithURLOverrides(url);

setTheme(settings.theme);

if (settings.game.length > 0 && settings.engine.length > 0) {
settingError = false;
playbackState.load(fetch, settings);
initWindowMessages();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically what was in load before

Moved it to onMount to quarantee it runs only in the browser, so we can use window.location.href to get the current url, with searchParams!

@@ -63,7 +80,7 @@
/>

<div use:resize={{ f: onResize }} class="h-full w-full flex flex-col items-center justify-center">
{#if data.settingError}
{#if settingError}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These no longer come from data they are 'top level' to this component!

@@ -7,7 +7,9 @@
{ value: 18, text: "Fast" }
];

function navigateBack() {
function navigateBack(e: MouseEvent) {
e.preventDefault();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this when I left settings it seemed to actually hit the href which didn't have the game query param

Adding this allowed me to go back and forth between the settings page and the game page

@coreyja coreyja marked this pull request as ready for review January 20, 2024 21:28
@coreyja coreyja merged commit cfdcbba into main Jan 20, 2024
4 checks passed
@coreyja coreyja deleted the ca/main/fix-deploys branch January 20, 2024 23:09
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

Successfully merging this pull request may close these issues.

2 participants