@prunellejs/spa 0.1.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/llms.txt +12 -3
- package/package.json +2 -2
- package/src/client.ts +31 -6
- package/src/index.ts +15 -2
package/llms.txt
CHANGED
|
@@ -10,11 +10,13 @@
|
|
|
10
10
|
|
|
11
11
|
## Partial Swaps (`regions()`)
|
|
12
12
|
|
|
13
|
-
Pair `regions()` in `prunelle.config.ts` with `<Region name="pane" as="section" ...attrs>` from `prunelle` (server-only)
|
|
13
|
+
Pair `regions()` in `prunelle.config.ts` with `<Region name="pane" as="section" ...attrs>` from `prunelle` (server-only like `Head`), which renders `<section data-pru-region="pane">` (a `<div>` without `as`). Its children are read only when the page or the requested region needs them.
|
|
14
14
|
|
|
15
|
-
- `<a href="/items?p=2" data-pru-target="results">`: sends `x-pru-region: results
|
|
15
|
+
- `<a href="/items?p=2" data-pru-target="results">`: sends `x-pru-region: results`, and the server answers with the region's inner HTML alone (no layouts, no document; `ctx.region` tells `load()` which region was asked for; `vary: x-pru-region`). The client swaps it in, updates `document.title` from `x-pru-title` (the page's `<Head><title>` with the title template), pushes history (back/forward re-fetch and restore the title), moves focus to the region, announces its `aria-label`, and sets `aria-busy="true"` after 150 ms, cleared on swap or failure.
|
|
16
|
+
- Without JS, or when the fetch fails or the response lacks the region, the link or form loads the whole page.
|
|
16
17
|
- `data-pru-append` on a targeted `<a>`: appends response HTML, removes clicked link (Load more), replaces history entry.
|
|
17
|
-
- `<form method="get" data-pru-target="results">`: swaps on submit; add `data-pru-live` to also swap while typing (200 ms debounce, one history entry per typing burst).
|
|
18
|
+
- `<form method="get" data-pru-target="results">`: swaps on submit; add `data-pru-live` to also swap while typing (200 ms debounce, one history entry per typing burst, which a 1 s pause ends).
|
|
19
|
+
- Nested regions each take their own name. Asking for the inner one renders it alone (header `x-pru-region: results/sets`, outermost first, computed by the client); asking for the outer one renders it with a fresh inner; an outer swap drops in-flight fetches of the regions inside it.
|
|
18
20
|
- `data-pru-sync="q tag"` on a link or input/select: keeps query params or field values outside the region in sync after every swap.
|
|
19
21
|
- `[data-pru-prefetch]` ancestor: prefetches targeted links on hover/focus (`data-no-prefetch` opts a link out).
|
|
20
22
|
- `<form method="post" data-pru-target="results">`: posts through `fetch`, follows the redirect and swaps the region from the final page; without JS it posts normally. `data-pru-live` is refused on it.
|
|
@@ -26,6 +28,13 @@ Pair `regions()` in `prunelle.config.ts` with `<Region name="pane" as="section"
|
|
|
26
28
|
- The regions client loads only on routes whose tree renders a `<Region>`: every other page ships none of it (plugin field `regionClientEntry`).
|
|
27
29
|
- Dispatches `window` event `pru:region` with `{ detail: { name, url } }`. `prunelle check` verifies every `data-pru-target` matches a `<Region>` in `src/`.
|
|
28
30
|
- `data-pru-keep-scroll="<key>"` on any element: its scroll offset survives region swaps and `spa()` swaps, and is kept per history entry.
|
|
31
|
+
- `await refreshRegion(name)` (from `@prunellejs/spa/regions`) redraws a region of the current address from script, after a background `fetch` to an action for instance: no history entry, focus left alone, prefetch cache skipped, `pru:region` fired.
|
|
32
|
+
|
|
33
|
+
## Mistakes
|
|
34
|
+
|
|
35
|
+
- Compile errors: `<Region>` without `name`, `data-pru-target` on anything but `<a>`/`<form>`, `data-pru-live` on a non-GET form, `data-pru-append`/`data-pru-live` without `data-pru-target`, a region inside a region of the same name.
|
|
36
|
+
- `prunelle check`: a `data-pru-target` naming no `<Region>` in `src/`.
|
|
37
|
+
- Runtime throws: two regions of one name on a page, a requested region the route does not render.
|
|
29
38
|
|
|
30
39
|
## Default: MPA plus `regions()`
|
|
31
40
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/spa",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Opt-in SPA navigation with persistent islands plugin for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -26,6 +26,6 @@
|
|
|
26
26
|
"directory": "packages/plugin-spa"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"prunelle": "^0.
|
|
29
|
+
"prunelle": "^0.6.0"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/src/client.ts
CHANGED
|
@@ -22,7 +22,21 @@ const PRUNELLE_BUILTIN_STYLES = new Set([
|
|
|
22
22
|
])
|
|
23
23
|
|
|
24
24
|
// One-time token and OAuth redirect endpoints must hit the server once via normal navigation, never via SPA fetch.
|
|
25
|
-
const SKIP_SPA_PATH_RE = /(?:^|\/)
|
|
25
|
+
const SKIP_SPA_PATH_RE = /(?:^|\/)(?:oauth(?:\/|$)|verify-email(?:\/|$)|magic-link\/verify(?:\/|$))/
|
|
26
|
+
|
|
27
|
+
function isSkippedPath(pathname: string): boolean {
|
|
28
|
+
if (SKIP_SPA_PATH_RE.test(pathname))
|
|
29
|
+
return true
|
|
30
|
+
const raw = document.querySelector('meta[name="pru-spa-skip"]')?.getAttribute('content')
|
|
31
|
+
if (!raw)
|
|
32
|
+
return false
|
|
33
|
+
for (const entry of raw.split(',')) {
|
|
34
|
+
const prefix = entry.trim()
|
|
35
|
+
if (prefix && (pathname === prefix || pathname.startsWith(`${prefix}/`)))
|
|
36
|
+
return true
|
|
37
|
+
}
|
|
38
|
+
return false
|
|
39
|
+
}
|
|
26
40
|
|
|
27
41
|
// Inline <style> blocks present on initial document load already have their SHA-256 hashes in this document's CSP.
|
|
28
42
|
const initialInlineStyles = new Set<string>(
|
|
@@ -61,14 +75,14 @@ function hasBlockedInlineStyle(nextDoc: Document, csp: string | null | undefined
|
|
|
61
75
|
}
|
|
62
76
|
|
|
63
77
|
function isEligibleLink(link: HTMLAnchorElement): URL | undefined {
|
|
64
|
-
if (link.target || link.hasAttribute('download') || link.
|
|
78
|
+
if (link.target || link.hasAttribute('download') || link.closest('[data-no-spa]') !== null)
|
|
65
79
|
return undefined
|
|
66
80
|
const url = new URL(link.href, location.href)
|
|
67
81
|
if (url.origin !== location.origin)
|
|
68
82
|
return undefined
|
|
69
83
|
if (url.protocol !== 'http:' && url.protocol !== 'https:')
|
|
70
84
|
return undefined
|
|
71
|
-
if (
|
|
85
|
+
if (isSkippedPath(url.pathname))
|
|
72
86
|
return undefined
|
|
73
87
|
return url
|
|
74
88
|
}
|
|
@@ -257,10 +271,17 @@ export async function navigate(url: string, push = true, restore?: EntryScroll,
|
|
|
257
271
|
}
|
|
258
272
|
const { html, url: finalUrl, csp } = fetched
|
|
259
273
|
|
|
274
|
+
const nextFullUrl = hash && !finalUrl.includes('#') ? `${finalUrl}${hash}` : finalUrl
|
|
275
|
+
const nextParsed = new URL(nextFullUrl, location.href)
|
|
276
|
+
// A server redirect may land on a foreign origin: fall back to a full navigation before touching the DOM or history.
|
|
277
|
+
if (nextParsed.origin !== location.origin) {
|
|
278
|
+
location.assign(nextFullUrl)
|
|
279
|
+
return
|
|
280
|
+
}
|
|
281
|
+
|
|
260
282
|
const nextDoc = new DOMParser().parseFromString(html, 'text/html')
|
|
261
283
|
resolveSuspenseTemplates(nextDoc)
|
|
262
284
|
|
|
263
|
-
const nextFullUrl = hash && !finalUrl.includes('#') ? `${finalUrl}${hash}` : finalUrl
|
|
264
285
|
if (hasBlockedInlineStyle(nextDoc, csp)) {
|
|
265
286
|
if (push)
|
|
266
287
|
location.assign(nextFullUrl)
|
|
@@ -275,8 +296,6 @@ export async function navigate(url: string, push = true, restore?: EntryScroll,
|
|
|
275
296
|
return
|
|
276
297
|
}
|
|
277
298
|
|
|
278
|
-
const nextParsed = new URL(nextFullUrl, location.href)
|
|
279
|
-
|
|
280
299
|
const applySwap = () => {
|
|
281
300
|
if (push)
|
|
282
301
|
saveScroll()
|
|
@@ -319,6 +338,12 @@ export async function navigate(url: string, push = true, restore?: EntryScroll,
|
|
|
319
338
|
// A page module the next page loads that this one did not (regions' client) runs now: scripts of a parsed page never do.
|
|
320
339
|
for (const script of nextDoc.head.querySelectorAll<HTMLScriptElement>('script[type="module"][src]'))
|
|
321
340
|
void import(new URL(script.getAttribute('src') ?? '', location.href).href)
|
|
341
|
+
const fb = nextDoc.querySelector<HTMLScriptElement>('script[data-kui-fallback]')?.dataset.kuiFallback
|
|
342
|
+
if (fb && !('command' in HTMLButtonElement.prototype && 'closedBy' in HTMLDialogElement.prototype && typeof CSS !== 'undefined' && CSS.supports?.('position-area:block-end'))) {
|
|
343
|
+
const fbUrl = new URL(fb, location.href)
|
|
344
|
+
if (fbUrl.origin === location.origin)
|
|
345
|
+
void import(fbUrl.href)
|
|
346
|
+
}
|
|
322
347
|
|
|
323
348
|
if (restore !== undefined) {
|
|
324
349
|
restoreScroll(restore)
|
package/src/index.ts
CHANGED
|
@@ -3,8 +3,14 @@
|
|
|
3
3
|
import type { BuildPluginContext, PrunellePlugin } from 'prunelle/config'
|
|
4
4
|
import { resolve } from 'node:path'
|
|
5
5
|
import { definePlugin } from 'prunelle/config'
|
|
6
|
+
import { escAttr } from 'prunelle/server'
|
|
6
7
|
import { checkRegionTargets } from './check'
|
|
7
8
|
|
|
9
|
+
export interface SpaOptions {
|
|
10
|
+
/** Path prefixes that must always perform a full browser navigation instead of an SPA swap (e.g. custom auth base paths). */
|
|
11
|
+
skipPaths?: readonly string[] | undefined
|
|
12
|
+
}
|
|
13
|
+
|
|
8
14
|
// Shipped as a same-origin stylesheet so navigating from an island-less page to an island/Suspense page
|
|
9
15
|
// never needs to inject an unhashed inline <style> tag blocked by the initial page's strict CSP.
|
|
10
16
|
const SPA_ISLAND_CSS = 'pru-island,pru-slot,pru-s{display:contents}'
|
|
@@ -13,11 +19,18 @@ function emitIslandStylesheet(ctx: BuildPluginContext): void {
|
|
|
13
19
|
ctx.addStyle(ctx.emitAsset('pru-island.css', SPA_ISLAND_CSS))
|
|
14
20
|
}
|
|
15
21
|
|
|
16
|
-
export function spa(): PrunellePlugin {
|
|
22
|
+
export function spa(options?: SpaOptions): PrunellePlugin {
|
|
23
|
+
const skipPaths = options?.skipPaths
|
|
24
|
+
?.map(p => p.trim().replace(/\/+$/, ''))
|
|
25
|
+
.filter(p => p.length > 0)
|
|
17
26
|
return definePlugin({
|
|
18
27
|
name: '@prunellejs/spa',
|
|
19
28
|
clientEntry: resolve(import.meta.dir, 'client.ts'),
|
|
20
|
-
build
|
|
29
|
+
build(ctx) {
|
|
30
|
+
emitIslandStylesheet(ctx)
|
|
31
|
+
if (skipPaths && skipPaths.length > 0)
|
|
32
|
+
ctx.addHead(`<meta name="pru-spa-skip" content="${escAttr(skipPaths.join(','))}">`)
|
|
33
|
+
},
|
|
21
34
|
})
|
|
22
35
|
}
|
|
23
36
|
|