I'm using Svelte and Sapper for a project. Let's say I have some code that needs to read a cookie before it runs, and that code is in a route at say /profile
or something.
My understanding is that Sapper provides no guarantees where the code will run. If I put the code in regular <script>
tags or maybe an onMount
block, when a user requests /profile
directly from the server, the code still execute on the server (and fail) but then execute again on the client:
<script>
import { getCookie } from "../../myUtilities.js";
const myCookieValue = getCookie("myCookie");
async function myRuntimeAction() {
let res = fetch(`https://www.example.com/api/${myCookieValue}`);
...
}
</script>
<form on:submit|preventDefault={myRuntimeAction}>
<button>
Take Action!
</button>
</form>
Is there an idiomatic Svelte / Sapper way to guarantee code only runs client-side, when it has access to cookies?
way to guarantee code only runs client-side
JS files send from server are runned at client side, cookies are send to server with request - this is all you need. – Elephantiasis