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

service-worker-i18n-redirect

v1.0.0

Published

i18n redirection using Workbox

Downloads

5

Readme

Service Worker Internationalization Redirect

Service Worker Internationalization Redirect (i18nR) provides logic to redirect a request based on the URL prefix pattern often found in internationalized URLs; for instance en/contact for the English version of a Contact URL and es/contact for the Spanish version of that same page.

Requirements

The only requirement for this to work properly is the structure of URLs to be the following: {{langcode}}/url/piece. This means that the only part of a URL that should be different for different languages is {{langcode}}; if other parts of the URL are different this redirection won't work.

In addition to the URL, an object that includes a get property with the following signature is needed to pass into the handler, and the preferred language needs to be available at the 'lang' key:

class PreferenceStore {
  constructor() {
    this.store = {};
  }

  async get(key) {
    return this.store[key];
  }
}

This module makes available a class that can be used, backed by idb-keyval, that can be used for this, includes both a get and set method, and can be used in both the main thread and in the Service Worker. Importing it via import { preferences } from 'service-worker-i18n-redirect/preferences'; makes it available.

Usage

Service Worker Internationalization Redirect is designed to work with service workers built with Workbox 5.0.0-rc.1 or greater. To use it, import it and use it as a handler for your route:

import { StaleWhileRevalidate } from 'workbox-strategies';
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
import { i18nHandler } from 'service-worker-i18n-redirect';
import { preferences } from 'service-worker-i18n-redirect/preferences';
import { registerRoute } from 'workbox-routing';

// Create a caching strategy
const htmlCachingStrategy = new StaleWhileRevalidate({
  cacheName: 'pages-cache',
  plugins: [
    new CacheableResponsePlugin({
      statuses: [200, 301, 404],
    }),
  ],
});

// Create an array of languages
const languages = ['en', 'es', 'fr', 'de', 'ko'];

// Use it for navigations
registerRoute(({ request }) => request.mode === 'navigate', i18nHandler(languages, preferences, htmlCachingStrategy));

To prevent a redirect, append ?locale_redirect=false to a URL.

Related

  • Rollup Plugin Workbox Inject - Allow Workbox to inject precache into a Rollup compiled service worker
  • Service Worker Includes - Caching Strategy plugin for including content when served from a Service Worker and differentially caching dynamic and static pieces of an HTML response.