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

teleport-kit

v0.0.3

Published

The current web-development world pride itself of blurring the line between backend and frontend and this is mostly done throw a process known as hydration. Your components are executed on the server and the html document is sent with all the information

Downloads

21

Readme

teleport-kit

The current web-development world pride itself of blurring the line between backend and frontend and this is mostly done throw a process known as hydration. Your components are executed on the server and the html document is sent with all the information already in it. Later on components are re-executed on the client and listeners are attached to the elements to allow for a reactive environment without the need of a full page reload.

This is generally not a problem but it can become a problem if the functions invoked are non deterministic. An example of this could be Math.random() or crypto.randomUUID or even simpler new Date(). Between the two executions (the server and the client) those two values could (and most likely will) change creating in the best case a random flicker in the UI, in the worst an hydration mismatch (even tho Svelte is pretty good at dealing with those 😎)

teleport-kit aim to solve this issue allowing you to specify variables that will be initialized with a value on the server and will have the same value once hydrated on the client!

Warning

This package is meant to be used with Svelte-Kit as the name suggest. Because it uses api that are only present in Svelte-Kit it will not work in your normal svelte project.

MIT License

npm

npm

GitHub last commit

Installation

Install teleport-kit with npm

  npm install teleport-kit@latest

Setup

After the installation of teleport-kit you need to do an extra step to allow the library to work for you: create or update your hooks.server.ts file.

export { handle } from 'teleport-kit';

if you already have another server handle already in your project you can make use of the sequence helper from Sveltekit (link to docs)

import { sequence } from '@sveltejs/kit/hooks';
import { handle as teleportkit } from 'teleport-kit';

export const handle = sequence(teleportkit, ({ event, resolve }) => {
	// your logic here
	return resolve(event);
});

Usage/Examples

Once you've done with this setup you can start using the teleport function inside your svelte components

<script lang="ts">
	import { teleport } from 'teleport-kit';

	let random = teleport('random', () => Math.random());
</script>

My random value: {random}

as you can see there's another small catch to use this library: you need to provide a unique name for your teleported variable in the form of the first argument of the teleport function. Is important that this name is:

  • the same on the server and on the client
  • unique for your whole route (this means every teleported variable including the ones in child layouts and components)

It's also important that the value you return is serializable by devalue (it's the same serialization library Svelte-kit uses...if you can return it from a server load function you can teleport it with teleport-kit!)

How it works?

I know you are curious!

  1. The handle function that you exports from hooks.server.ts wrap your whole Svelte-kit application in an AsyncLocalStorage so that every request will have it's own context.
  2. Through the use of esm exports the teleport function that you execute on the server will update the storage assigning a property based on the name you provide with the value returned from your function and will return the value itself.
  3. After the request is resolved in the server hook the html is transformed to append a small <script> tag containing the teleported values.
  4. On the client the teleport function (different from the one on the server thanks to esm exports) will read the content of the aforementioned script tag and return the value present there instead of calling the function. If no value is found the function is called and the value returned.
  5. the already served value is deleted from the script tag to avoid serving the same value if the component is unmounted and remounted.

And that's it!

Contributing

Contributions are always welcome!

For the moment there's no code of conduct neither a contributing guideline but if you found a problem or have an idea feel free to open an issue

If you want the fastest way to open a PR try out Codeflow

Open in Codeflow

Authors