1
Fork 0
svelte-tiny-spa/README.md

47 lines
2 KiB
Markdown
Raw Permalink Normal View History

2019-11-29 14:57:39 +00:00
# svelte-tiny-spa
2019-11-29 16:36:35 +00:00
A tiny unframework for making SPAs in Svelte.
## Overview
2020-11-11 10:48:01 +00:00
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 and upon `popstate` events. It also optionally provides a mechanism for hosts which do not support falling back to `index.html` for missing files.
2019-11-29 16:36:35 +00:00
## Setup
If your host supports falling back to `index.html` for requests of all unknown files, you should use that feature. Copy [`location.js`](location.js) into your project, and you're done.
### If your host does not support falling back to `index.html`
2020-11-11 10:48:01 +00:00
You'll use a custom 404 page that redirects to the root and encodes the original URL in a query parameter. The root page then rewrites the URL using `history.replaceState()`.
2019-11-29 16:36:35 +00:00
Add [`404.html`](404.html) to your project at the appropriate path. Uncomment the lines at the top of [`location.js`](location.js) to handle the rewrite after the redirection.
## Usage
```js
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:
2019-12-17 13:00:01 +00:00
1. The link is same-origin.
1. The link is not merely a `#hash` change.
1. The link does not have a `target` attribute.
1. The link does not have a `download` attribute.
1. The link was left clicked.
2020-08-09 12:52:42 +00:00
1. The link was not clicked while pressing Alt/Ctrl/Meta/Shift.
1. The link click was not `preventDefault`ed.
2019-12-17 13:00:01 +00:00
The store also contains `goto(url, replace)` and `scroll()` methods.
- `goto(url, replace)` navigates to a URL.
- `url` is resolved according to normal rules.
- `replace` is a boolean indicating whether to use `history.pushState()` or `history.replaceState()`.
- `scroll()` scrolls to either the top of the page or the element indicated by the `#hash` portion of the URL. This should be called after you've finished rendering a page.
2019-11-29 19:27:52 +00:00
2019-11-29 16:36:35 +00:00
## License
[MIT](LICENSE)