Sapper: Client-only code

Created on 30 Mar 2018  路  4Comments  路  Source: sveltejs/sapper

I'm attempting to write a component that can update my user's history so they can easily deep-link to a set of filter settings as they update the settings. What's the best way to write client-only code that relies on window being available?

Most helpful comment

Also, any code inside component lifecycle hooks (oncreate and ondestroy) or any methods will only run on the client

All 4 comments

Nevermind, I've figured out how to get this to work without erroring out. Sapper includes process.browser for client-rendered code. I've wrapped my browser-only code in a check for if(process.browser === true), and all is well

Also, any code inside component lifecycle hooks (oncreate and ondestroy) or any methods will only run on the client

@Rich-Harris are there any hooks like oncreate/ondestroy that will run only on server side?

@jakubsacha it's better to open a new issue and link to any old issues when you're asking something new. For general support questions maybe the Discord chatroom or Stack Overflow are a better choice.


The oncreate and ondestroy are runtime only and don't make sense at all in a SSR senario. Maybe <script context="module"> could be useful in your case? See:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SARFEX picture SARFEX  路  3Comments

keyvan-m-sadeghi picture keyvan-m-sadeghi  路  4Comments

Rich-Harris picture Rich-Harris  路  4Comments

Rich-Harris picture Rich-Harris  路  4Comments

freedmand picture freedmand  路  4Comments