sveltekit-preview-mode icon indicating copy to clipboard operation
sveltekit-preview-mode copied to clipboard

[Major] Remove Shared State on the Server

Open bertybot opened this issue 6 months ago • 1 comments

Description

Current preview mode implementation works by sharing isPreview store on the server and the client, Sveltekit docs explain some of the issues this can cause . Also more explanation in the original issue #14

Fix

isPreview is now scoped to the individual request by passing it down through locals.isPreview instead of storing it in a global store that is shared on the server. This is a breaking change as now any code using the store will not work, and have to be updated.

Other fixes

Added a building check to the hook so that prerendering can be supported, also added section to readme to outline how prerendering can be supported.

Summary of fixes

  • Removed isPreview() and preview store
  • Updated all example code to new pattern
  • Updated PreviewBanner to check page store instead of isPreview store.
  • Added Building Check to hook
  • Updated pnpm
  • Updated Readme

Considerations

  • The example code that didn't take this pattern into account originally is bit messier now since we constantly have to pass in isPreview I tried to avoid changing the original style. I usually do this pattern cleaner using classes so we can reuse the client and preview mode can be stored.
const client = new CmsClient(isPreview)

await client.getPosts(slug)

Let me know what you prefer.

  • Also I did not have access to the env variables for the CMS so I was unable to accurately test the examples. I however, tested on my setup and it works.

bertybot avatar Dec 22 '23 16:12 bertybot