create-shapio 0.5.0 → 0.5.2
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/package.json +1 -1
- package/templates/astro/.env.example +6 -0
- package/templates/astro/README.md +19 -1
- package/templates/astro/astro.config.mjs +7 -0
- package/templates/astro/package.json +8 -4
- package/templates/astro/scripts/incrementalBuild.ts +15 -3
- package/templates/astro/scripts/incrementalCheck.ts +218 -0
- package/templates/astro/scripts/seed.ts +77 -24
- package/templates/astro/scripts/smokeDrafts.ts +109 -0
- package/templates/astro/src/layouts/Base.astro +11 -3
- package/templates/astro/src/lib/cacheKey.test.ts +63 -0
- package/templates/astro/src/lib/cacheKey.ts +57 -0
- package/templates/astro/src/lib/config.ts +33 -3
- package/templates/astro/src/lib/layoutInputs.ts +13 -0
- package/templates/astro/src/lib/renderTime.test.ts +31 -0
- package/templates/astro/src/lib/renderTime.ts +24 -0
- package/templates/astro/src/lib/shapio.ts +88 -64
- package/templates/astro/src/lib/site.ts +4 -0
- package/templates/astro/src/lib/snapshotResolver.test.ts +64 -0
- package/templates/astro/src/lib/snapshotResolver.ts +52 -0
- package/templates/astro/src/lib/types.ts +0 -9
- package/templates/astro/src/pages/[locale]/[...slug].astro +23 -3
- package/templates/astro/src/pages/[locale]/articles/[slug].astro +22 -2
- package/templates/astro/src/pages/[locale]/articles/index.astro +7 -1
- package/templates/astro/src/pages/[locale]/colophon.astro +1 -1
- package/templates/astro/src/pages/build.json.ts +9 -2
- package/templates/astro/src/styles/site.css +14 -0
- package/templates/next/.env.example +6 -0
- package/templates/next/README.md +9 -0
- package/templates/next/next.config.ts +5 -1
- package/templates/next/package.json +4 -3
- package/templates/next/scripts/seed.ts +77 -24
- package/templates/next/scripts/smokeDrafts.ts +109 -0
- package/templates/next/src/app/[locale]/layout.tsx +3 -0
- package/templates/next/src/app/site.css +14 -0
- package/templates/next/src/components/DraftsBadge/index.tsx +10 -0
- package/templates/next/src/lib/config.ts +13 -1
- package/templates/next/src/lib/revalidation.ts +4 -1
- package/templates/next/src/lib/shapio.ts +24 -4
- package/templates/next/src/lib/shapioClient.ts +6 -4
- package/templates/next/src/lib/site.ts +4 -0
- package/templates/next/src/lib/snapshotResolver.test.ts +64 -0
- package/templates/next/src/lib/snapshotResolver.ts +52 -0
- package/templates/sveltekit/.env.example +6 -0
- package/templates/sveltekit/README.md +9 -0
- package/templates/sveltekit/package.json +7 -4
- package/templates/sveltekit/scripts/seed.ts +77 -24
- package/templates/sveltekit/scripts/smokeDrafts.ts +109 -0
- package/templates/sveltekit/src/env.ts +2 -0
- package/templates/sveltekit/src/lib/server/config.ts +11 -1
- package/templates/sveltekit/src/lib/server/shapio.ts +28 -13
- package/templates/sveltekit/src/lib/server/snapshotResolver.test.ts +64 -0
- package/templates/sveltekit/src/lib/server/snapshotResolver.ts +52 -0
- package/templates/sveltekit/src/lib/site.css +14 -0
- package/templates/sveltekit/src/lib/site.ts +4 -0
- package/templates/sveltekit/src/routes/[locale]/+layout.server.ts +3 -2
- package/templates/sveltekit/src/routes/[locale]/+layout.svelte +3 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which publication snapshot the site reads, and for how long a read is reused. A build pins one snapshot
|
|
3
|
+
* for all its pages (SHAPIO_SNAPSHOT, else the current one when the build starts). The dev server reads the
|
|
4
|
+
* current snapshot again once the last read is older than DEV_WINDOW_MS, so a publish shows on reload while
|
|
5
|
+
* the reads of one page render still share a snapshot.
|
|
6
|
+
*/
|
|
7
|
+
export const DEV_WINDOW_MS = 1000;
|
|
8
|
+
|
|
9
|
+
/** How long a cached read is reused: for the whole process in a build, DEV_WINDOW_MS in dev. */
|
|
10
|
+
export const cacheWindow = (dev: boolean): number => (dev ? DEV_WINDOW_MS : Number.POSITIVE_INFINITY);
|
|
11
|
+
|
|
12
|
+
/** `read`, reused for `windowMs` after it starts. A read that fails is not kept: the next call tries again. */
|
|
13
|
+
export const cachedRead = <T>(
|
|
14
|
+
read: () => Promise<T>,
|
|
15
|
+
windowMs: number,
|
|
16
|
+
now: () => number = Date.now,
|
|
17
|
+
): (() => Promise<T>) => {
|
|
18
|
+
let cached: { value: Promise<T>; at: number } | undefined;
|
|
19
|
+
return () => {
|
|
20
|
+
const at = now();
|
|
21
|
+
if (cached && at - cached.at < windowMs) {
|
|
22
|
+
return cached.value;
|
|
23
|
+
}
|
|
24
|
+
const entry = { value: read(), at };
|
|
25
|
+
cached = entry;
|
|
26
|
+
entry.value.catch(() => {
|
|
27
|
+
if (cached === entry) {
|
|
28
|
+
cached = undefined;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return entry.value;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type SnapshotResolverOptions = {
|
|
36
|
+
/** True under the dev server. */
|
|
37
|
+
dev: boolean;
|
|
38
|
+
/** SHAPIO_SNAPSHOT: when set, the site shows exactly this snapshot, in dev too. */
|
|
39
|
+
configured: number | undefined;
|
|
40
|
+
/** Reads Shapio's current publication snapshot. */
|
|
41
|
+
current: () => Promise<number>;
|
|
42
|
+
now?: () => number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** The snapshot every delivery read sends. */
|
|
46
|
+
export const createSnapshotResolver = ({
|
|
47
|
+
dev,
|
|
48
|
+
configured,
|
|
49
|
+
current,
|
|
50
|
+
now,
|
|
51
|
+
}: SnapshotResolverOptions): (() => Promise<number>) =>
|
|
52
|
+
configured === undefined ? cachedRead(current, cacheWindow(dev), now) : () => Promise.resolve(configured);
|
|
@@ -197,3 +197,17 @@ main {
|
|
|
197
197
|
text-align: center;
|
|
198
198
|
font-weight: 600;
|
|
199
199
|
}
|
|
200
|
+
/* Drafts mode (SHAPIO_DRAFTS=true): a corner badge, so a drafts build is never mistaken for the real site. */
|
|
201
|
+
.drafts-badge {
|
|
202
|
+
position: fixed;
|
|
203
|
+
right: 1rem;
|
|
204
|
+
bottom: 1rem;
|
|
205
|
+
z-index: 10;
|
|
206
|
+
margin: 0;
|
|
207
|
+
background: #78350f;
|
|
208
|
+
color: #fef3c7;
|
|
209
|
+
padding: 0.25rem 0.75rem;
|
|
210
|
+
border-radius: 999px;
|
|
211
|
+
font-size: 0.875rem;
|
|
212
|
+
font-weight: 700;
|
|
213
|
+
}
|
|
@@ -19,6 +19,8 @@ const STRINGS = {
|
|
|
19
19
|
by: 'By',
|
|
20
20
|
switchTo: 'Français',
|
|
21
21
|
skipToContent: 'Skip to content',
|
|
22
|
+
drafts: 'Drafts',
|
|
23
|
+
draftsNote: 'Drafts mode: this server shows saved drafts, not the published site.',
|
|
22
24
|
footer: 'Built with Shapio',
|
|
23
25
|
previewBanner: 'Preview: this is a draft, not the published page.',
|
|
24
26
|
previewLoading: 'Loading the draft…',
|
|
@@ -32,6 +34,8 @@ const STRINGS = {
|
|
|
32
34
|
by: 'Par',
|
|
33
35
|
switchTo: 'English',
|
|
34
36
|
skipToContent: 'Aller au contenu',
|
|
37
|
+
drafts: 'Brouillons',
|
|
38
|
+
draftsNote: 'Mode brouillons : ce serveur montre les brouillons enregistrés, pas le site publié.',
|
|
35
39
|
footer: 'Construit avec Shapio',
|
|
36
40
|
previewBanner: 'Aperçu : ceci est un brouillon, pas la page publiée.',
|
|
37
41
|
previewLoading: 'Chargement du brouillon…',
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { error } from '@sveltejs/kit';
|
|
2
|
-
import { siteUrl } from '#lib/server/config.ts';
|
|
2
|
+
import { isDraftsMode, siteUrl } from '#lib/server/config.ts';
|
|
3
3
|
import { getSite, getSiteSettings } from '#lib/server/shapio.ts';
|
|
4
4
|
import { isLocale, stringsFor } from '#lib/site.ts';
|
|
5
5
|
import type { LayoutServerLoad } from './$types';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* The locale's site settings (the siteSettings singleton) and UI strings, for the header and footer; the site's
|
|
9
|
-
* SEO defaults and public URL, for every page's <head
|
|
9
|
+
* SEO defaults and public URL, for every page's <head>; whether drafts mode is on, for its badge.
|
|
10
10
|
*/
|
|
11
11
|
export const load: LayoutServerLoad = async ({ params }) => {
|
|
12
12
|
if (!isLocale(params.locale)) {
|
|
@@ -18,5 +18,6 @@ export const load: LayoutServerLoad = async ({ params }) => {
|
|
|
18
18
|
settings: await getSiteSettings(params.locale),
|
|
19
19
|
site: await getSite(),
|
|
20
20
|
siteUrl: siteUrl() ?? null,
|
|
21
|
+
drafts: isDraftsMode(),
|
|
21
22
|
};
|
|
22
23
|
};
|
|
@@ -28,5 +28,8 @@
|
|
|
28
28
|
<footer class="site-footer">
|
|
29
29
|
<p>{siteName} · {data.settings?.footer ?? data.strings.footer} · SvelteKit</p>
|
|
30
30
|
</footer>
|
|
31
|
+
{#if data.drafts}
|
|
32
|
+
<p class="drafts-badge" title={data.strings.draftsNote} data-shapio-drafts="">{data.strings.drafts}</p>
|
|
33
|
+
{/if}
|
|
31
34
|
<!-- Visual editing (@shapio/visual) plugs in here: its script and the data-shapio attributes on content arrive
|
|
32
35
|
with Shapio's visual-editing SDK, together with draft preview. -->
|