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

@elo7/fastify-svelte-partial-hydration

v2.1.0

Published

Fastify plugin for using Svelte with partial hydration support

Downloads

4

Readme

Fastify Svelte Partial Hydration

This plugin will help you add partially hydrated pages to your fastify-svelte application.

Table of contents

  1. Installation
  2. Usage

Installation

To install this plugin, run the command:

# npm
npm install --save @elo7/fastify-svelte-partial-hydration
# yarn
yarn add @elo7/fastify-svelte-partial-hydration

Usage

To use this plugin, we have to pass through 4 steps: to register, to make the hydratable components globally callable, to add the hydration script and to use the hydration component.

Steps

  1. Register
  2. Register hydratable-components
  3. Hydration script
  4. Hydration component

Register

The first part is to register the plugin with the fastify-svelte, by passing the default export of the plugin to the plugins array for the fastify-svelte:

import sveltePartialHydration from
'@elo7/fastify-svelte-parital-hydration`;

app.register(sveltePlugin, {
	plugins: [sveltePartialHydration],
});

Register hydratable components

The second part is divided by two steps: generate scripts that will make the components globally available and the register these scripts on your page.

The hydratable components need to generate scripts that make them globally available, below, you will see an example on how to do that using the window object and the virtual module plugin for Rollup. Note that the generated scripts are placed under

/static/js/mobile/components/<component-name>/template.js

const componentsPath = 'src/views/mobile/components/';
const hydrateTemplates = glob.sync(`${componentsPath}/**/template.svelte`);

const clientSideConfig = (template) => {
	const { component } = template.match(/.+\/mobile\/.+\/(?<component>.+)\/template.svelte/).groups;

	return {
		input: component,
		output: {
			file: template.replace('src', 'static/js').replace('svelte', 'js'),
			format: 'iife',
		},
		plugins: [
			virtual({
				[component]: `
					import ${component} from './${template}';
					window[${component}] = ${component};
				`,
			}),
			svelte({
				css: false,
				dev,
				hydratable: true,
			}),
			resolve(),
			commonjs(),
		],
	};
};

export default [
	...hydrateTemplates.map(clientSideConfig);
];

This plugins makes the key componentsToHydrate available, which will contain all components's names that should be hydrated. In this step, we are calling the scripts generated by the rollup configuration above:

// template.js
export default ({ head, css, componentsToHydrate, content }) => `
// ...
<body>
	// ...

	${componentsToHydrate.map(component => `<script async src='/static/js/views/mobile/components/${component}/template.js'></script>`)}
</body>
`;

Hydration script

The third part is to create your component hydrator, which is a function that will receive the name of the component, the props and the DOM's element where the component will be mounted, and call the Svelte component constructor:

import hydrate from '@elo7/fastify-svelte-partial-hydration/hydrate';

const componentBuilder = ({ component, element, props }) => {
	new window[component]({
		hydrate: true,
		props: JSON.parse(props),
		target: element,
	});
};

hydrate(componentBuilder);

Register the script above on your template.js;

// template.js
export default (...) => `
// ...
<body>
	// ...
	<script async src='/static/js/hydrate.js'></script>
</body>
`;

NOTE: The code above assumes that the client side compilation of your svelte plugins are stored within the window object, this can be achieved by using virtual modules with rollup. There is an example on how to do this in the Register hydratable components section.

Hydration component

The fourth and final part is using the Hydrate Svelte component, you need to pass the name of the component, the component itself and the props object that will be passed to the components.

import Hydrate from '@elo7/fastify-svelte-partial-hydration/Hydrate.svelte';
import SearchBar from './SearchBar.svelte';

<Hydrate
	component={SearchBar}
	name="SearchBar"
	props={{
		autocomplete: true,
	}}
/>