@velocity-astro/cms-astro 0.1.0-beta.1 → 0.1.0-beta.11
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/dist/admin-files.d.ts +2 -2
- package/dist/admin-files.d.ts.map +1 -1
- package/dist/admin-files.js +13 -7
- package/dist/admin-files.js.map +1 -1
- package/dist/challenge.d.ts +35 -0
- package/dist/challenge.d.ts.map +1 -0
- package/dist/challenge.js +84 -0
- package/dist/challenge.js.map +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +4 -3
- package/dist/cli.js.map +1 -1
- package/dist/connection.d.ts +1 -0
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +31 -6
- package/dist/connection.js.map +1 -1
- package/dist/editor.d.ts +2 -0
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +9 -1
- package/dist/editor.js.map +1 -1
- package/dist/form-post.d.ts +9 -1
- package/dist/form-post.d.ts.map +1 -1
- package/dist/form-post.js +10 -3
- package/dist/form-post.js.map +1 -1
- package/dist/hosted.d.ts +76 -0
- package/dist/hosted.d.ts.map +1 -0
- package/dist/hosted.js +104 -0
- package/dist/hosted.js.map +1 -0
- package/dist/image-optimizer.d.ts +35 -0
- package/dist/image-optimizer.d.ts.map +1 -0
- package/dist/image-optimizer.js +76 -0
- package/dist/image-optimizer.js.map +1 -0
- package/dist/index.d.ts +12 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -3
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +2 -0
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +16 -1
- package/dist/init.js.map +1 -1
- package/dist/integration.d.ts +9 -0
- package/dist/integration.d.ts.map +1 -1
- package/dist/integration.js +15 -1
- package/dist/integration.js.map +1 -1
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +6 -2
- package/dist/link.js.map +1 -1
- package/dist/live-loader.d.ts +7 -3
- package/dist/live-loader.d.ts.map +1 -1
- package/dist/live-loader.js +12 -6
- package/dist/live-loader.js.map +1 -1
- package/dist/media-store.d.ts +15 -0
- package/dist/media-store.d.ts.map +1 -1
- package/dist/media-store.js +26 -2
- package/dist/media-store.js.map +1 -1
- package/dist/page-marks.d.ts +50 -0
- package/dist/page-marks.d.ts.map +1 -0
- package/dist/page-marks.js +90 -0
- package/dist/page-marks.js.map +1 -0
- package/dist/preview.d.ts +34 -2
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +81 -15
- package/dist/preview.js.map +1 -1
- package/dist/resend-mailer.d.ts +18 -1
- package/dist/resend-mailer.d.ts.map +1 -1
- package/dist/resend-mailer.js +24 -3
- package/dist/resend-mailer.js.map +1 -1
- package/dist/secret.d.ts +1 -1
- package/dist/secret.d.ts.map +1 -1
- package/dist/secret.js +3 -2
- package/dist/secret.js.map +1 -1
- package/dist/single.d.ts +18 -0
- package/dist/single.d.ts.map +1 -0
- package/dist/single.js +37 -0
- package/dist/single.js.map +1 -0
- package/dist/site-editor.d.ts +1 -1
- package/dist/site-editor.d.ts.map +1 -1
- package/dist/site-editor.js +36 -6
- package/dist/site-editor.js.map +1 -1
- package/dist/site-forms.d.ts +11 -0
- package/dist/site-forms.d.ts.map +1 -1
- package/dist/site-forms.js +37 -19
- package/dist/site-forms.js.map +1 -1
- package/dist/site-mail.d.ts +45 -0
- package/dist/site-mail.d.ts.map +1 -0
- package/dist/site-mail.js +84 -0
- package/dist/site-mail.js.map +1 -0
- package/dist/site-media.d.ts +5 -1
- package/dist/site-media.d.ts.map +1 -1
- package/dist/site-media.js +14 -5
- package/dist/site-media.js.map +1 -1
- package/dist/site-name.d.ts +9 -0
- package/dist/site-name.d.ts.map +1 -0
- package/dist/site-name.js +21 -0
- package/dist/site-name.js.map +1 -0
- package/dist/site-storage.d.ts +34 -0
- package/dist/site-storage.d.ts.map +1 -0
- package/dist/site-storage.js +118 -0
- package/dist/site-storage.js.map +1 -0
- package/package.json +11 -8
- package/routes/editor.ts +7 -6
- package/routes/form-client.js.ts +68 -0
- package/routes/media.ts +2 -2
- package/routes/preview-client.js.ts +11 -56
- package/routes/published.ts +2 -2
- package/routes/sitemap-live.xml.ts +4 -1
- package/routes/sites.ts +11 -0
package/routes/editor.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* What the editor's three routes share. Compiled with the site, like every file here, so it can
|
|
3
3
|
* read the settings the integration wrote and the site's own mailer.
|
|
4
4
|
*/
|
|
5
|
-
import { indexPages, pagesOrigin, siteEditorHost, withEditor, type EditorHost, type SiteEditorSettings } from '@velocity-astro/cms-astro';
|
|
5
|
+
import { currentSite, indexPages, pagesOrigin, siteCacheTag, siteEditorHost, withEditor, type EditorHost, type SiteEditorSettings } from '@velocity-astro/cms-astro';
|
|
6
6
|
import images from 'virtual:velocity-cms/images';
|
|
7
7
|
import mailer from 'virtual:velocity-cms/mailer';
|
|
8
8
|
import options from 'virtual:velocity-cms/options';
|
|
@@ -24,23 +24,24 @@ export const ownOrigin = (request: Request): string | null => pagesOrigin(reques
|
|
|
24
24
|
export async function showPublished(host: EditorHost, cache: PageCache, origin: string | null): Promise<void> {
|
|
25
25
|
const changed = host.takePublished();
|
|
26
26
|
if (!changed) return;
|
|
27
|
-
await cache.invalidate({ tags: [
|
|
27
|
+
await cache.invalidate({ tags: [siteCacheTag()] });
|
|
28
28
|
if (origin === null) return;
|
|
29
29
|
await indexPages(origin, changed).catch((error: unknown) => console.error('[velocity-cms] Search was not updated.', error));
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
/** When
|
|
33
|
-
|
|
32
|
+
/** When each site's editor was last found not to be set up, so a site without one is not asked on every request. */
|
|
33
|
+
const notSetUpAt = new Map<string, number>();
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Publishes what was set to go out by now, and shows it. A site whose editor is not set up has
|
|
37
37
|
* nothing scheduled, so that is passed over in silence.
|
|
38
38
|
*/
|
|
39
39
|
export async function publishDue(request: Request, cache: PageCache, options: { now?: boolean } = {}): Promise<void> {
|
|
40
|
-
|
|
40
|
+
const site = currentSite()?.id ?? '';
|
|
41
|
+
if (Date.now() - (notSetUpAt.get(site) ?? 0) < 30_000) return;
|
|
41
42
|
const host = await siteEditorHost(request, settings).catch(() => null);
|
|
42
43
|
if (!host) {
|
|
43
|
-
notSetUpAt
|
|
44
|
+
notSetUpAt.set(site, Date.now());
|
|
44
45
|
return;
|
|
45
46
|
}
|
|
46
47
|
await host.publishDue(options);
|
package/routes/form-client.js.ts
CHANGED
|
@@ -15,6 +15,11 @@ export const prerender = false;
|
|
|
15
15
|
* - a submit button may carry `data-cms-form-sending`, its words while the form is on its way.
|
|
16
16
|
* They replace the button's words, or those of the `[data-cms-form-label]` inside it.
|
|
17
17
|
*
|
|
18
|
+
* - `[data-cms-form-challenge]`: where the check that the sender is a person is drawn, when an
|
|
19
|
+
* owner set one up (Cloudflare Turnstile). Without the mark it is drawn above the submit
|
|
20
|
+
* button. A site with a content security policy allows `https://challenges.cloudflare.com`
|
|
21
|
+
* for scripts and frames.
|
|
22
|
+
*
|
|
18
23
|
* Every word is written as text. Nothing the visitor typed comes back from the site.
|
|
19
24
|
*/
|
|
20
25
|
const SCRIPT = `
|
|
@@ -39,6 +44,66 @@ function say(form, state, message) {
|
|
|
39
44
|
}
|
|
40
45
|
}
|
|
41
46
|
|
|
47
|
+
const CHALLENGE = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
|
|
48
|
+
const challenges = new WeakMap();
|
|
49
|
+
const waiting = new WeakMap();
|
|
50
|
+
let challengeScript;
|
|
51
|
+
|
|
52
|
+
/** Loads the challenge's script once, however many forms a page has. */
|
|
53
|
+
function challengeReady() {
|
|
54
|
+
challengeScript ??= new Promise((resolve, reject) => {
|
|
55
|
+
if (window.turnstile) return resolve();
|
|
56
|
+
const script = document.createElement('script');
|
|
57
|
+
script.src = CHALLENGE;
|
|
58
|
+
script.async = true;
|
|
59
|
+
script.onload = () => resolve();
|
|
60
|
+
script.onerror = () => {
|
|
61
|
+
challengeScript = undefined;
|
|
62
|
+
reject(new Error('The challenge could not be loaded.'));
|
|
63
|
+
};
|
|
64
|
+
document.head.append(script);
|
|
65
|
+
});
|
|
66
|
+
return challengeScript;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Draws the check that the sender is a person, when the form carries a site key for one. */
|
|
70
|
+
function challenge(form) {
|
|
71
|
+
const siteKey = form.elements.namedItem('_challenge')?.value;
|
|
72
|
+
if (!siteKey || challenges.has(form)) return;
|
|
73
|
+
let place = form.querySelector('[data-cms-form-challenge]');
|
|
74
|
+
if (!place) {
|
|
75
|
+
place = document.createElement('div');
|
|
76
|
+
place.dataset.cmsFormChallenge = '';
|
|
77
|
+
const button = form.querySelector('[type="submit"]');
|
|
78
|
+
if (button) button.before(place);
|
|
79
|
+
else form.append(place);
|
|
80
|
+
}
|
|
81
|
+
challenges.set(
|
|
82
|
+
form,
|
|
83
|
+
challengeReady()
|
|
84
|
+
.then(() => window.turnstile.render(place, { sitekey: siteKey, theme: 'auto', size: 'flexible', language: document.documentElement.lang || 'auto', callback: () => waiting.get(form)?.() }))
|
|
85
|
+
// Without the challenge the form still posts. The site decides what to make of it.
|
|
86
|
+
.catch(() => undefined),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Waits, for a while, for the sender to pass a challenge that is still being worked out. */
|
|
91
|
+
async function challengePassed(form) {
|
|
92
|
+
const widget = await challenges.get(form);
|
|
93
|
+
if (widget == null || window.turnstile.getResponse(widget)) return;
|
|
94
|
+
await new Promise((resolve) => {
|
|
95
|
+
waiting.set(form, resolve);
|
|
96
|
+
setTimeout(resolve, 15000);
|
|
97
|
+
});
|
|
98
|
+
waiting.delete(form);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** An answer to the challenge is used once: a new one is asked for after every post. */
|
|
102
|
+
async function challengeAgain(form) {
|
|
103
|
+
const widget = await challenges.get(form);
|
|
104
|
+
if (widget != null) window.turnstile?.reset(widget);
|
|
105
|
+
}
|
|
106
|
+
|
|
42
107
|
async function send(form) {
|
|
43
108
|
const button = form.querySelector('[type="submit"]');
|
|
44
109
|
const words = button?.querySelector('[data-cms-form-label]') ?? button;
|
|
@@ -50,6 +115,7 @@ async function send(form) {
|
|
|
50
115
|
if (button.dataset.cmsFormSending) words.textContent = button.dataset.cmsFormSending;
|
|
51
116
|
}
|
|
52
117
|
try {
|
|
118
|
+
await challengePassed(form);
|
|
53
119
|
const response = await fetch(form.action, { method: 'POST', headers: { accept: 'application/json' }, body: new URLSearchParams(new FormData(form)) });
|
|
54
120
|
const answer = await response.json().catch(() => null);
|
|
55
121
|
if (response.ok && answer?.ok) {
|
|
@@ -75,6 +141,7 @@ async function send(form) {
|
|
|
75
141
|
} catch {
|
|
76
142
|
say(form, 'failed', form.dataset.cmsFormFailed ?? '');
|
|
77
143
|
} finally {
|
|
144
|
+
void challengeAgain(form);
|
|
78
145
|
if (button) {
|
|
79
146
|
button.disabled = false;
|
|
80
147
|
words.textContent = label;
|
|
@@ -86,6 +153,7 @@ function start() {
|
|
|
86
153
|
for (const form of document.querySelectorAll('form[data-cms-form]')) {
|
|
87
154
|
if (form.dataset.cmsFormReady) continue;
|
|
88
155
|
form.dataset.cmsFormReady = 'true';
|
|
156
|
+
challenge(form);
|
|
89
157
|
form.addEventListener('submit', (event) => {
|
|
90
158
|
event.preventDefault();
|
|
91
159
|
if (form.dataset.cmsFormState !== 'sending') void send(form);
|
package/routes/media.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { uploadedFile } from '@velocity-astro/cms-astro';
|
|
|
4
4
|
export const prerender = false;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* An image someone uploaded in the editor. It may be cached, and says so itself: a file never
|
|
7
|
+
* An image someone uploaded in the editor, or with `?w=` a smaller copy of it. It may be cached, and says so itself: a file never
|
|
8
8
|
* changes once it is kept.
|
|
9
9
|
*/
|
|
10
|
-
export const GET: APIRoute = ({ params }) => uploadedFile(params.file ?? '');
|
|
10
|
+
export const GET: APIRoute = ({ params, url }) => uploadedFile(params.file ?? '', url.searchParams.get('w'));
|
|
@@ -1,63 +1,18 @@
|
|
|
1
1
|
import type { APIRoute } from 'astro';
|
|
2
|
+
import { EDITOR_PREVIEW_VERSION, PREVIEW_BRIDGE_SCRIPT } from '@velocity-astro/cms-astro';
|
|
2
3
|
|
|
3
4
|
export const prerender = false;
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
* What the page beside the editor's form runs. The preview adds it to
|
|
7
|
-
* does nothing unless the page is in a frame of the same site.
|
|
7
|
+
* What the page beside the editor's form runs (`src/preview-bridge.ts`). The preview adds it to
|
|
8
|
+
* that page only, and it does nothing unless the page is in a frame of the same site.
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* would not
|
|
10
|
+
* The page asks for it by the mark of this version, and a browser keeps that: the page is drawn
|
|
11
|
+
* again with every change in the form, and a script that is at hand runs before the page is seen,
|
|
12
|
+
* so the page comes back where it was. Asked for any other way, it is fetched each time, since a
|
|
13
|
+
* copy kept from before an upgrade would not know what the editor now says to it.
|
|
13
14
|
*/
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const sectionOf = (target) => (target instanceof Element ? target.closest('[data-cms-section]') : null);
|
|
19
|
-
|
|
20
|
-
let marked = null;
|
|
21
|
-
const mark = (section) => {
|
|
22
|
-
if (marked === section) return;
|
|
23
|
-
if (marked) marked.style.outline = marked.style.outlineOffset = marked.style.cursor = '';
|
|
24
|
-
marked = section;
|
|
25
|
-
if (!section) return;
|
|
26
|
-
section.style.outline = '2px solid Highlight';
|
|
27
|
-
section.style.outlineOffset = '-2px';
|
|
28
|
-
section.style.cursor = 'pointer';
|
|
29
|
-
};
|
|
30
|
-
document.addEventListener('mouseover', (event) => mark(sectionOf(event.target)));
|
|
31
|
-
document.documentElement.addEventListener('mouseleave', () => mark(null));
|
|
32
|
-
|
|
33
|
-
// Nothing on the page goes anywhere: a click chooses the section it is in.
|
|
34
|
-
document.addEventListener('click', (event) => {
|
|
35
|
-
event.preventDefault();
|
|
36
|
-
const section = sectionOf(event.target);
|
|
37
|
-
if (section) say({ type: 'section', id: section.dataset.cmsSection });
|
|
38
|
-
}, true);
|
|
39
|
-
document.addEventListener('submit', (event) => event.preventDefault(), true);
|
|
40
|
-
|
|
41
|
-
addEventListener('message', (event) => {
|
|
42
|
-
if (event.origin !== origin || event.source !== window.parent || event.data?.source !== 'velocity-cms') return;
|
|
43
|
-
if (event.data.type !== 'show') return;
|
|
44
|
-
const section = [...document.querySelectorAll('[data-cms-section]')].find((candidate) => candidate.dataset.cmsSection === event.data.id);
|
|
45
|
-
if (!section) return;
|
|
46
|
-
// Room for a header that stays at the top of the page.
|
|
47
|
-
section.style.scrollMarginTop = '6rem';
|
|
48
|
-
section.scrollIntoView({ block: 'start' });
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const key = 'velocity-cms:scroll:' + location.pathname;
|
|
52
|
-
try {
|
|
53
|
-
const kept = Number(sessionStorage.getItem(key));
|
|
54
|
-
if (kept > 0) scrollTo({ top: kept, behavior: 'instant' });
|
|
55
|
-
addEventListener('pagehide', () => sessionStorage.setItem(key, String(scrollY)));
|
|
56
|
-
} catch {}
|
|
57
|
-
|
|
58
|
-
say({ type: 'ready' });
|
|
59
|
-
}
|
|
60
|
-
`;
|
|
61
|
-
|
|
62
|
-
export const GET: APIRoute = () =>
|
|
63
|
-
new Response(SCRIPT, { headers: { 'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': 'public, max-age=3600' } });
|
|
15
|
+
export const GET: APIRoute = ({ url }) => {
|
|
16
|
+
const kept = url.searchParams.get('v') === EDITOR_PREVIEW_VERSION ? 'private, max-age=31536000, immutable' : 'no-cache';
|
|
17
|
+
return new Response(PREVIEW_BRIDGE_SCRIPT, { headers: { 'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': kept } });
|
|
18
|
+
};
|
package/routes/published.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { APIRoute } from 'astro';
|
|
2
|
-
import { hasSiteSecret, indexPages, indexSite, invalidateLiveContent } from '@velocity-astro/cms-astro';
|
|
2
|
+
import { hasSiteSecret, indexPages, indexSite, invalidateLiveContent, siteCacheTag } from '@velocity-astro/cms-astro';
|
|
3
3
|
import { ownOrigin } from './editor.ts';
|
|
4
4
|
import { addresses, clientDir } from './pages.ts';
|
|
5
5
|
|
|
@@ -18,7 +18,7 @@ interface Notice {
|
|
|
18
18
|
export const POST: APIRoute = async ({ cache, request }) => {
|
|
19
19
|
if (!hasSiteSecret(request)) return new Response(null, { status: 404 });
|
|
20
20
|
invalidateLiveContent();
|
|
21
|
-
await cache.invalidate({ tags: [
|
|
21
|
+
await cache.invalidate({ tags: [siteCacheTag()] });
|
|
22
22
|
const { reindex } = (await request.json().catch(() => ({}))) as Notice;
|
|
23
23
|
const origin = ownOrigin(request);
|
|
24
24
|
if (!reindex || origin === null) return Response.json({});
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import type { APIRoute } from 'astro';
|
|
2
|
+
import { currentSite } from '@velocity-astro/cms-astro';
|
|
2
3
|
import { addresses } from './pages.ts';
|
|
3
4
|
|
|
4
5
|
export const prerender = false;
|
|
5
6
|
|
|
6
7
|
/** The live pages' share of the sitemap. Point `sitemap-index.xml` here with `customSitemaps`. */
|
|
7
8
|
export const GET: APIRoute = async ({ site, url }) => {
|
|
9
|
+
// On a server that runs many sites, the addresses are the asked site's, not the template's.
|
|
10
|
+
const base = currentSite()?.origin ?? site ?? url;
|
|
8
11
|
const urls = (await addresses())
|
|
9
12
|
.filter((address) => address.listed && !address.fixed)
|
|
10
|
-
.map((address) => `<url><loc>${new URL(address.path,
|
|
13
|
+
.map((address) => `<url><loc>${new URL(address.path, base).href}</loc></url>`);
|
|
11
14
|
const xml = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls.join('')}</urlset>`;
|
|
12
15
|
return new Response(xml, { headers: { 'Content-Type': 'application/xml; charset=utf-8' } });
|
|
13
16
|
};
|
package/routes/sites.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chooses the site each request is for, on a server that runs many sites from this one copy.
|
|
3
|
+
* Compiled with the site, like every file here, so the site's bundler reads the file that knows
|
|
4
|
+
* the sites; it runs first, before anything reads content.
|
|
5
|
+
*/
|
|
6
|
+
import { hostedRequest, useSiteRegistry } from '@velocity-astro/cms-astro';
|
|
7
|
+
import sites from 'virtual:velocity-cms/sites';
|
|
8
|
+
|
|
9
|
+
useSiteRegistry(sites);
|
|
10
|
+
|
|
11
|
+
export const onRequest = hostedRequest;
|