sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
17.81k stars 1.78k forks source link

export csr = '/custom.js' #12154

Closed multipliedtwice closed 3 weeks ago

multipliedtwice commented 3 weeks ago

Describe the problem

I have a simple statically generated landing page, that doesn't have much logic on it - just a basic dropdown menu.

Scheduler/entry/etc is an overkill for a simple landing page that needs to switch a couple of CSS classes

Describe the proposed solution

export csr = '/static.js'

where the end user could do some custom arbitrary JS that will be added as a script at the bottom of the page

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

Conduitry commented 3 weeks ago

You can already do this with <svelte:head><script>...</script></svelte:head> in the component and continuing to specify csr = false. Alternatively, if you want your script to run later, you can also put it anywhere in the component, as long as it's not a top-level element, which has special meaning to Svelte.

multipliedtwice commented 3 weeks ago
<div class="hidden">--custom.js--</div>

at any part of the page.

And then in hooks

responseBody = (await response.text())
            .replace("--custom.js--", '<script src="/custom.js"></script>')

do the trick