Menu

Publishing a single-page app

Put an app built with React, Vue, Svelte or any router of its own online, with every address loading the app and a cache that keeps new versions showing at once.

A single-page app is one index.html and its scripts: the app draws each page in the browser and changes the address itself (/settings, /photos/42). Opened directly, or reloaded, such an address names no file in the folder, so it needs one rule that serves index.html in its place. This recipe puts such an app online at your own hostname or a free address.

1. Build the app#

Build it the way its tools say (npm run build, for example). The result is a folder, often dist/ or build/, with index.html at its root.

Make sure the app's files are referred to from the root (/assets/app.js, not assets/app.js): a page such as /photos/42 loads them from there. Most build tools do this by default (a "base" or "public path" of /).

2. Add _redirects#

Put a file named _redirects in the built folder, next to index.html (most tools copy a public/ folder into the build as it is: put it there once):

/*    /index.html    200

Every address that names no file now loads your app, which shows the right page. The app's own files are served as they are: a rule only applies to an address that names nothing (Existing files win).

If your app has a not-found page of its own, it draws it itself: the gateway answers 200 for every address, as single-page apps need.

Build tools give the scripts and styles a new name at every build (app.3f9a1c.js). Those can be kept by browsers for a year; index.html must be checked on every visit, so a new version shows at once:

/*
  Cache-Control: no-cache

/assets/*
  ! Cache-Control
  Cache-Control: public, max-age=31536000, immutable

Use the folder your tool puts those files in (/assets/, /static/, /_app/). The guide explains the ! Cache-Control line.

4. Upload and check#

$ weird storage add ./dist

The upload prints the content's own address (https://<cid>.ipfs.<gateway domain>/); both files already apply there. Open it, go to a page inside the app and reload it: the app should come back on the same page.

5. Put it at your address#

$ weird storage domains attach app.example.ro <cid>

Or, on a free address, weird storage domains attach ana.e-weird.ro <cid>. A new version later is a new upload and a retarget:

$ weird storage add ./dist$ weird storage domains retarget app.example.ro <new cid>

What it costs#

Every address served through the rule is a page sent to a visitor, charged per MB like any other (What it costs). Keeping the app stored is charged by the hour per MB, as for any site (Storage and sites).

Credit charge: see the credit charges (contract recurring.ipfs-pin-hourly per_mb_served)

Each MB sent to visitors.