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

@amazeelabs/strangler-netlify

v1.2.1

Published

Netlify function for facading legacy systems.

Downloads

494

Readme

Strangler (Netlify)

Library that allows to create a Netlify function that implements the strangler fig pattern. It allows to layer a Netlify website on top of on or more legacy systems, gradually replacing them.

Concept

Every path that is not part of the Netlify website, is handed to a list of legacy systems. If a system is able to handle that path, the response will be proxied back to the client. If not, the next system will be tried. If none of the systems can handle the path, a 404 will be returned.

Usage

Add the library to your project:

pnpm add @amazeelabs/strangler-netlify

Create a Netlify function that uses the library:

// file: netlify/functions/strangler.ts
import { createStrangler } from '@amazeelabs/strangler-netlify';
import fs from 'fs';

// Read the static 404 page from the file system.
// Thats the page content the function will return if none
// of the legacy systems can handle the request.
const notFoundPage = fs.readFileSync('./public/404.html');

export const handler = createStrangler(
  [
    {
      // Specify a URL to the legacy system.
      url: 'https://legacy.web.site',
      // Optional function that can check if the current url even
      // applies for the system. If not, the system will be skipped.
      applies: (url) => url.pathname.startsWith('/redirect/'),
      // Optional function that can modify the response from the legacy system.
      // If the function returns undefined, the response will be ignored and the
      // next system will be tried.
      process: (response) =>
        [301, 302].includes(response.status) ? response : undefined,
    },
  ],
  notFoundPage,
);

Add a catchall-redirect to the _redirects, that will pass the request to the strangler function:

# Pass all unhandled requests to the strangler function.
/* /.netlify/functions/strangler 200

Warning:
The redirect must be the last line in the file.

Optimizations

The redirect will not be executed for any files that are part of the Netlify build and for paths that match redirects or rewrites in the _redirects file. To reduce the number of invocation of the function, it is recommended to add manual rewrites for any known paths that should be handled by the legacy systems.

# Rewrite uploaded Drupal files to Drupal directly.
/sites/default/files/* https://legacy.web.site/sites/default/files/:splat 200

# Pass all unhandled requests to the strangler function.
/* /.netlify/functions/strangler 200