0.9.0 • Published 1 year ago

next-static-manifest v0.9.0

Weekly downloads
31
License
MIT
Repository
-
Last release
1 year ago

Installation

$ npm install --save next-static-manifest
$ yarn add next-static-manifest

Usage

Let's take a look at this scenario, our application looks like this:

|-- pages
|   |-- index.js
|   |-- [...slug].js
|   |-- blog/posts/[id].js
|   |-- blog/posts/[id]/[authorId].js

We add next-static-manifest to our export script:

$ next build && next export && next-static-manifest

And after exporting our app, we get this:

|-- out
|   |-- next-static-manifest.json
|   |-- index.html
|   |-- [...slug].html
|   |-- blog/posts/[id].html
|   |-- blog/posts/[id]/[authorId].html

The next-static-manifest.json will look this:

[
  {
    "src": "/blog/posts/[id]/[authorId]",
    "dest": "/blog/posts/[id]/[authorId].html",
    "regex": "__REGEXP__:/^\\/blog\\/posts\\/([^/]+?)\\/([^/]+?)(?:\\/)?$/",
    "dynamic": true
  },
  {
    "src": "/blog/posts/[id]",
    "dest": "/blog/posts/[id].html",
    "regex": "__REGEXP__:/^\\/blog\\/posts\\/([^/]+?)(?:\\/)?$/",
    "dynamic": true
  },
  {
    "src": "/index",
    "dest": "/index.html",
    "regex": "__REGEXP__:/^\\/index(?:\\/)?$/",
    "dynamic": false
  },
  {
    "src": "/404",
    "dest": "/404.html",
    "regex": "__REGEXP__:/^\\/404(?:\\/)?$/",
    "dynamic": false
  },
  {
    "dynamic": false,
    "regex": "__REGEXP__:/^\\/(?:\\/)?$/",
    "src": "/",
    "dest": "index.html"
  },
  {
    "src": "/[...slug]",
    "dest": "/[...slug].html",
    "regex": "__REGEXP__:/^\\/(.+?)(?:\\/)?$/",
    "dynamic": true
  }
]

Without any type of infrastructure in place, we can't route users to our dynamically exported routes.

A request to: /blog/posts/123-456-789 wont match our filesystem location for the /blog/posts/[id].html file.

However, if we write a small Lambda@Edge function or use a Worker to ingest this file, we can automatically route users to the correct .html entrypoint.

How it works

next-static-manifest runs after your app has been exported by Next. We generate a next-static-manifest.json file that you can poll on Lambda@Edge or in your Worker, after deploying, your worker will get a new file and route accordingly.

In your function/worker, it's important to use decode when parsing the manifest file.

import { decode } from 'next-static-manifest';

fetch('.../next-static-manifest.json')
  .then((r) => r.json())
  .then((data) => decode(data));

We need to hydrate our data with decode, because regex isn't part of the JSON spec, so we have to encode/decode it ourselves.

Questions? Feedback? Please let me know

License (MIT)

WWWWWW||WWWWWW
 W W W||W W W
      ||
    ( OO )__________
     /  |           \
    /o o|    MIT     \
    \___/||_||__||_|| *
         || ||  || ||
        _||_|| _||_||
       (__|__|(__|__|

Copyright © 2020-present Jack Hanford, jackhanford@gmail.com

0.9.0

1 year ago

0.8.5

2 years ago

0.8.4

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.8.3

2 years ago

0.8.2

2 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.3.0

3 years ago

0.4.0

3 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago