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

multipage-fallback

v0.1.2

Published

Fallback unhandled requests to the nearest index

Downloads

11

Readme

🪀 Multipage Fallback

Fallback unhandled requests to the nearest specified index within your single or multi page applications

npm downloads size license

Introduction

The multipage-fallback package is intended for managing unhandled requests in multipage applications. It automatically serves the nearest index file based on your configuration, with or without redirecting requests. This is especially beneficial when developing web applications that consist of multiple pages and routes.

With multipage-fallback, allows your application to gracefully handle situations where a specific route is not found. Instead of showing a 404 error page, the package will fallback to the nearest index file, providing a smooth user experience.

To use multipage-fallback, you simply need to install the package and configure it according to your project's needs. The package integrates seamlessly with popular frameworks and build tools, making it easy to incorporate into your existing development workflow.

How does it work?

Suppose you have the following source code structure:

├── package.json
├── vite.config.js
├── index.html
├── main.js
└── nested
    ├── index.html
    ├── nested.js
    └── subroute
        └─ index.html

multipage-fallback attempts to locate an index.html file that best matches the requested path. For instance, if the path is /nested/subroute/my/secret/path, the following file paths will be considered in order:

  1. nested/subroute/my/secret/index.html
  2. nested/subroute/my/index.html
  3. nested/subroute/index.html
  4. nested/index.html
  5. index.html

In this situation, nested/subroute/index.html is selected because that file exists. If it were not available, the fallback would then be nested/index.html, and if that were also absent, it would further fall back to index.html at the root level.

The reason nested/subroute/my/secret/path/index.html is not considered in this process is due to the absence of a trailing /. If you want to include this path, you can set the strictTrailingSlash option to false.

Use with Express

📄 express.js

Always remember to include the multipage-fallback middleware at the very end of your app.

import express from "express"
import { expressMiddleware as multipageFallback } from "multipage-fallback"

const app = express()

// your routes/middlewares

app.use(multipageFallback())

app.listen(3000)

⚙️ Options

| Name | Default Value | Valid Types | | ---------------------- | ------------------ | ------------------ | | root | "./dist/" | string | | index | "index.html" | string | | nestingLimit | 8 | number | | sendOptions | SendOptions | SendOptions | | strictTrailingSlash | true | boolean | | redirect | false | boolean | | redirectRegex | /(?:)/ | RegExp | | redirectRemoveFileName | true | boolean |

Use with Hono

📄 hono.js

Always remember to include the multipage-fallback middleware at the very end of your app.

import { Hono } from "hono"
import { serve } from "@hono/node-server"
import { serveStatic } from "@hono/node-server/serve-static"
import { honoMiddleware as multipageFallback } from "multipage-fallback"

const app = new Hono()

// your routes/middlewares

app.use("/*", multipageFallback())

serve({ fetch: app.fetch, port: 3000 })

⚙️ Options

| Name | Default Value | Valid Types | | ---------------------- | -------------- | ----------- | | root | "./dist/" | string | | index | "index.html" | string | | nestingLimit | 8 | number | | strictTrailingSlash | false | boolean | | redirect | false | boolean | | redirectRegex | /(?:)/ | RegExp | | redirectRemoveFileName | true | boolean |

Use with Vite

📄 vite.config.js

import { vitePlugin as multipageFallback } from "multipage-fallback"

export default defineConfig({
  appType: "mpa",
  plugins: [multipageFallback()],
})

⚙️ Options

| Name | Default Value | Valid Types | | ---------------------- | -------------- | ----------- | | index | "index.html" | string | | nestingLimit | 8 | number | | strictTrailingSlash | true | boolean | | redirect | false | boolean | | redirectRegex | /(?:)/ | RegExp | | redirectRemoveFileName | true | boolean |

License

MIT