1
Fork 0
A tiny unframework for making SPAs in Svelte
Go to file
Conduitry 832216cba5 attach event handlers after location store is subscribed to 2019-11-30 11:25:24 -05:00
404.html add 404 redirect 2019-11-29 10:17:05 -05:00
LICENSE initial commit 2019-11-29 09:57:39 -05:00
README.md document goto method 2019-11-29 14:27:52 -05:00
location.js attach event handlers after location store is subscribed to 2019-11-30 11:25:24 -05:00

README.md

svelte-tiny-spa

A tiny unframework for making SPAs in Svelte.

Overview

This library intercepts clicks on internal links and turns them into history.pushState() calls. The browser's location object is exposed through a Svelte store, which updates upon these intercepted clicks or on other popstate events. It also optionally provides a mechanism for hosts which do not support falling back to index.html for missing files.

Setup

If your host supports falling back to index.html for requests of all unknown files, you should use that feature. Copy location.js into your project, and you're done.

If your host does not support falling back to index.html

You'll use a custom 404 page that redirects to the root and encodes the original URL is a query parameter. The root page then rewrites the URL with history.replaceState().

Add 404.html to your project at the appropriate path. Uncomment the lines at the top of location.js to handle the rewrite after the redirection.

Usage

import location from '.../location.js';

This is a Svelte store containing the location object, and updates whenever a link click is intercepted or a popstate event occurs.

Interception occurs when all of the following are met:

  1. The link is same-origin
  2. The link does not have a target attribute
  3. The link does not have a download attribute
  4. The link is left clicked
  5. Ctrl/Cmd/Shift are not pressed when clicking the link

The store also contains a goto(url, replace) method. This accepts two arguments:

  • url, a string containing a URL to programmatically navigate to, which is resolved according to normal rules
  • replace, a boolean indicating whether to use history.pushState() or history.replaceState()

Non-replacing goto calls will also scroll to the top of the page.

License

MIT