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

Docs are missing new script injection tools #101

Open
fnimick opened this issue Nov 19, 2024 · 5 comments
Open

Docs are missing new script injection tools #101

fnimick opened this issue Nov 19, 2024 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@fnimick
Copy link
Contributor

fnimick commented Nov 19, 2024

Change Type

Addition

Proposed Changes

Should ideally have an example of how to do this in hooks.server.ts, and add docs to the new generateSetInitialModeExpression function.

I can take a stab at this later if you'd like.

@fnimick fnimick added the documentation Improvements or additions to documentation label Nov 19, 2024
@huntabyte
Copy link
Member

I was actually thinking of creating a more complete helper that can just be dropped into the sequence. it would use generateSetInitialModeExpression under the hood but would take care of the other stuff.

If you'd like to take a stab at that given you have experience implementing it in user land, that would be great!

@fnimick
Copy link
Contributor Author

fnimick commented Nov 26, 2024

@huntabyte well, it requires manual changes to app.html, so I'm not sure a "drop this in your hook sequence" is a great UX since the failure mode when you haven't modified it is not obvious.

Out of curiosity, are there any other use cases that you know of for JS injection into the initially rendered script, like the FOUC here? I wonder if there should be a community standard for these things that you could then use in a hook like this one, with a single app.html modification for all of them.

@huntabyte
Copy link
Member

Why would it require manual changes to app.html? We should be able to add the appropriate class/theme via the html prop on the transformPageChunk no?

@fnimick
Copy link
Contributor Author

fnimick commented Nov 26, 2024

I don't think that works, since we need to inject javascript to run to read the client's local storage and set the class correctly, no? We don't have access to this at initial render time.

My solution is to add the following to app.html:

    <script nonce="%sveltekit.nonce%">
      %modewatcher.snippet%
    </script>

And the following server-side hook:

const injectHeadScripts: Handle = async ({ event, resolve }) => {
  return resolve(event, {
    transformPageChunk: ({ html }) => {
      return html.replace("%modewatcher.snippet%", generateSetInitialModeExpression({}));
    },
  });
};

@huntabyte
Copy link
Member

We'd use a cookie for the server side instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants