npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sveltekit-preview-mode

v0.1.8

Published

CMS Preview Mode for SvelteKit

Downloads

15

Readme

SvelteKit Preview Mode

SvelteKit Preview Mode is a library for SvelteKit that helps you easily enable preview mode for content management systems (CMSs). With this library, you can preview draft or unpublished content without any hassle. This is intended to be a SvelteKit equivelant of the Next.js Preview Mode.

Demo (using Hygraph)

Installation

To install SvelteKit Preview Mode, run the following command:

pnpm add sveltekit-preview-mode -D

Usage

To use SvelteKit Preview Mode, simply import it in your SvelteKit application:

// src/hooks.server.ts
import type { Handle } from "@sveltejs/kit";
import { PREVIEW_SECRET } from "$env/static/private";
import previewMode from "sveltekit-preview-mode";

export const handle: Handle = previewMode({
  previewSecret: PREVIEW_SECRET,
});

Don't forget to set the PREVIEW_SECRET environment variable. This can be any string you'd like. If you need to add additional handlers, you can do so using the sequence helper function.

In order to share the preview status in the client, you'll need to add this to +layout.server.ts:

// src/routes/+layout.server.ts
import type { LayoutServerLoad } from "./$types";

/**
 * Return the `exitPreviewQueryParam` and `isPreview` values so that they can be referenced in client-side code.
 */
export const load = (({ locals: { exitPreviewQueryParam, isPreview } }) => {
  return {
    exitPreviewQueryParam,
    isPreview,
  };
}) satisfies LayoutServerLoad;

And then in +layout.ts we need to update the store to hold the preview status:

// src/routes/+layout.ts
import { setPreview } from "sveltekit-preview-mode";
import type { LayoutLoad } from "./$types";

/**
 * Call `setPreview` with the `isPreview` value so that the `isPreview` value can be referenced in client-side code.
 */
export const load = (({ data: { isPreview } }) => {
  setPreview(isPreview);
}) satisfies LayoutLoad;

Displaying Preview Status

To display a banner when preview mode is enabled, import the PreviewMode banner component into +layout.svelte:

<!-- src/routes/+layout.svelte -->
<script lang="ts">
  import { PreviewBanner } from "sveltekit-preview-mode";
</script>

<PreviewBanner />

You can now retrieve the preview status by importing the isPreview function, and call it to retrieve the current preview status.

Enabling Preview Mode

To enable preview mode, add the query parameter ?secret=PREVIEW_SECRET to any route. This will then check for a matching secret, and update the isPreview value, and set a cookie to securely persist preview mode between sessions.

Disabling Preview Mode

To disable preview mode, add the query parameter ?exit-preview to any route. This will then update the isPreview value, and update the cookie value.

Examples

Options

The previewMode handle function has a few options that can be set:

interface PreviewModeOptions {
  previewSecret: string;
  cookieName?: string;
  cookieOpts?: CookieSerializeOptions;
  exitPreviewQueryParam?: string;
  secretTokenQueryParam?: string;
}

| Property | Type | Default | Description | | --- | --- | --- | --- | | previewSecret | string | N/A | This is the query parameter value, which needs to match in order to enable preview mode. | | cookieName? | string | __preview_mode | The name of the cookie that is created to store the preview mode state. | | cookieOpts? | CookieSerializeOptions | 🔗 | Options for the cookie we create. | | exitPreviewQueryParam? | string | exit-preview | The query param that should be present to exit preview mode. | | secretTokenQueryParam? | string | secret | The query param that should be used to enter preview mode. |

Contributing

If you'd like to contribute to SvelteKit Preview Mode, feel free to open an issue or pull request on our GitHub repository.

Credits

This project is inspired by the Vercel Next.js team's approach in providing Preview Mode functionality. Thanks for their hard work! 🖤

License

SvelteKit Preview Mode is released under the ISC License.