fontdue-js 3.0.0-alpha10 → 3.0.0-alpha11
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/__generated__/FontdueAdminToolbarQuery.graphql.d.ts +20 -0
- package/dist/__generated__/FontdueAdminToolbarQuery.graphql.js +80 -0
- package/dist/__tests__/createFontdueFetch.test.js +104 -0
- package/dist/__tests__/imageLoader.test.js +62 -0
- package/dist/__tests__/networkFetch.test.js +22 -0
- package/dist/__tests__/preview.test.js +96 -0
- package/dist/__tests__/previewServer.test.js +118 -0
- package/dist/components/BuyButton/index.d.ts +2 -2
- package/dist/components/BuyButton/index.js +3 -3
- package/dist/components/CharacterViewer/index.d.ts +2 -2
- package/dist/components/CharacterViewer/index.js +3 -3
- package/dist/components/ConfigContext.d.ts +10 -0
- package/dist/components/ConfigContext.js +5 -1
- package/dist/components/FontdueAdminToolbar/index.js +26 -25
- package/dist/components/FontdueProvider/index.js +2 -2
- package/dist/components/NewsletterSignup/index.d.ts +2 -2
- package/dist/components/NewsletterSignup/index.js +2 -2
- package/dist/components/TestFontsForm/index.d.ts +2 -2
- package/dist/components/TestFontsForm/index.js +2 -2
- package/dist/components/TypeTester/TypeTesterStandalone.d.ts +2 -2
- package/dist/components/TypeTester/TypeTesterStandalone.js +2 -2
- package/dist/components/TypeTesters/index.d.ts +2 -2
- package/dist/components/TypeTesters/index.js +3 -3
- package/dist/loadFontdueProviderQuery.d.ts +2 -1
- package/dist/loadFontdueProviderQuery.js +5 -2
- package/dist/next/image-loader.js +22 -3
- package/dist/preview/constants.d.ts +3 -0
- package/dist/preview/constants.js +18 -0
- package/dist/preview/index.d.ts +49 -0
- package/dist/preview/index.js +164 -0
- package/dist/preview/server.d.ts +20 -0
- package/dist/preview/server.js +89 -0
- package/dist/relay/environment.d.ts +6 -0
- package/dist/relay/environment.js +4 -1
- package/dist/relay/loadSerializableQuery.d.ts +13 -3
- package/dist/relay/loadSerializableQuery.js +2 -0
- package/dist/relay/serverConfig.d.ts +3 -0
- package/dist/relay/serverConfig.js +26 -1
- package/dist/server/index.d.ts +25 -0
- package/dist/server/index.js +101 -0
- package/package.json +4 -1
|
@@ -1,41 +1,42 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import _FontdueAdminToolbarTokenMutation from "../../__generated__/FontdueAdminToolbarTokenMutation.graphql.js";
|
|
4
|
-
import
|
|
5
|
-
import React, { useEffect, useState } from 'react';
|
|
4
|
+
import _FontdueAdminToolbarQuery from "../../__generated__/FontdueAdminToolbarQuery.graphql.js";
|
|
5
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
6
6
|
import { commitMutation, fetchQuery, graphql, useRelayEnvironment } from 'react-relay';
|
|
7
|
-
|
|
7
|
+
import ConfigContext from '../ConfigContext.js';
|
|
8
|
+
import { PREVIEW_ENDPOINT, PREVIEW_MARKER_COOKIE } from '../../preview/constants.js';
|
|
9
|
+
// Admin-only affordance for logged-in foundry admins: reveal unpublished
|
|
8
10
|
// ("hidden") fonts across the whole storefront. Storefront pages are
|
|
9
11
|
// server-rendered sessionless and cached, and the provider's own query is
|
|
10
12
|
// preloaded server-side without the session — so `adminUser` can't ride it.
|
|
11
|
-
//
|
|
13
|
+
// Admin presence is detected here, client-side, where the request carries
|
|
12
14
|
// the session; the public never sees this control.
|
|
13
15
|
//
|
|
14
16
|
// The reveal itself is not done here: entering preview brokers a short-lived
|
|
15
17
|
// admin token and hands it to the framework's preview route, which switches
|
|
16
18
|
// the whole app to a live, token-bearing render. Exiting clears it. Public
|
|
17
19
|
// renders never touch any of this and stay static and cached.
|
|
18
|
-
const
|
|
20
|
+
const adminQuery = (_FontdueAdminToolbarQuery.hash && _FontdueAdminToolbarQuery.hash !== "8660b45f086137d249eee82459ad648d" && console.error("The definition of 'FontdueAdminToolbarQuery' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."), _FontdueAdminToolbarQuery);
|
|
19
21
|
|
|
20
|
-
// createAdminToken mints a short-lived, stateless admin token from the
|
|
22
|
+
// createAdminToken mints a short-lived, stateless admin token from the admin
|
|
21
23
|
// session; it's handed to the preview route to enter draft mode.
|
|
22
24
|
const tokenMutation = (_FontdueAdminToolbarTokenMutation.hash && _FontdueAdminToolbarTokenMutation.hash !== "2b82f195747c86d50fd5884d76f3b709" && console.error("The definition of 'FontdueAdminToolbarTokenMutation' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."), _FontdueAdminToolbarTokenMutation);
|
|
23
25
|
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//
|
|
30
|
-
// route. Lets this client component tell preview is on; the token itself is
|
|
31
|
-
// only ever read server-side.
|
|
32
|
-
const PREVIEW_MARKER = 'fontdue_preview';
|
|
26
|
+
// The preview entry/exit route and the readable marker cookie are part of the
|
|
27
|
+
// portable preview contract — see fontdue-js/preview. The route path defaults
|
|
28
|
+
// to '/api/preview' but is configurable via FontdueConfig.preview.endpoint so
|
|
29
|
+
// non-Next apps can mount it wherever their router expects (the Next adapter's
|
|
30
|
+
// draft-mode route, an Astro APIRoute, an RR7 action — all honor the same
|
|
31
|
+
// POST-token-to-enter / DELETE-to-exit contract).
|
|
33
32
|
function hasPreviewCookie() {
|
|
34
|
-
return typeof document !== 'undefined' && document.cookie.split('; ').some(c => c === `${
|
|
33
|
+
return typeof document !== 'undefined' && document.cookie.split('; ').some(c => c === `${PREVIEW_MARKER_COOKIE}=1`);
|
|
35
34
|
}
|
|
36
35
|
export default function FontdueAdminToolbar() {
|
|
36
|
+
var _useContext$preview;
|
|
37
37
|
const environment = useRelayEnvironment();
|
|
38
|
-
const
|
|
38
|
+
const previewEndpoint = ((_useContext$preview = useContext(ConfigContext).preview) === null || _useContext$preview === void 0 ? void 0 : _useContext$preview.endpoint) ?? PREVIEW_ENDPOINT;
|
|
39
|
+
const [adminName, setAdminName] = useState(null);
|
|
39
40
|
const [ready, setReady] = useState(false);
|
|
40
41
|
const [open, setOpen] = useState(false);
|
|
41
42
|
const [previewing, setPreviewing] = useState(false);
|
|
@@ -45,14 +46,14 @@ export default function FontdueAdminToolbar() {
|
|
|
45
46
|
setPreviewing(hasPreviewCookie());
|
|
46
47
|
// network-only: the provider's preloaded query already wrote a sessionless
|
|
47
48
|
// `viewer` to the store, so we must go to the network (with the session)
|
|
48
|
-
// rather than read that cached,
|
|
49
|
-
const subscription = fetchQuery(environment,
|
|
49
|
+
// rather than read that cached, admin-blind copy.
|
|
50
|
+
const subscription = fetchQuery(environment, adminQuery, {}, {
|
|
50
51
|
fetchPolicy: 'network-only'
|
|
51
52
|
}).subscribe({
|
|
52
53
|
next: data => {
|
|
53
54
|
var _data$viewer;
|
|
54
55
|
const admin = (_data$viewer = data.viewer) === null || _data$viewer === void 0 ? void 0 : _data$viewer.adminUser;
|
|
55
|
-
if (admin)
|
|
56
|
+
if (admin) setAdminName(admin.name ?? 'admin');
|
|
56
57
|
},
|
|
57
58
|
error: () => setReady(true),
|
|
58
59
|
complete: () => setReady(true)
|
|
@@ -60,10 +61,10 @@ export default function FontdueAdminToolbar() {
|
|
|
60
61
|
return () => subscription.unsubscribe();
|
|
61
62
|
}, [environment]);
|
|
62
63
|
|
|
63
|
-
// Hidden until we've confirmed
|
|
64
|
-
if (!ready || !
|
|
64
|
+
// Hidden until we've confirmed an admin session. The public always lands here.
|
|
65
|
+
if (!ready || !adminName) return null;
|
|
65
66
|
async function startPreview(token) {
|
|
66
|
-
const res = await fetch(
|
|
67
|
+
const res = await fetch(previewEndpoint, {
|
|
67
68
|
method: 'POST',
|
|
68
69
|
headers: {
|
|
69
70
|
'content-type': 'application/json'
|
|
@@ -104,7 +105,7 @@ export default function FontdueAdminToolbar() {
|
|
|
104
105
|
async function exitPreview() {
|
|
105
106
|
setBusy(true);
|
|
106
107
|
setError(null);
|
|
107
|
-
await fetch(
|
|
108
|
+
await fetch(previewEndpoint, {
|
|
108
109
|
method: 'DELETE'
|
|
109
110
|
});
|
|
110
111
|
window.location.reload();
|
|
@@ -121,7 +122,7 @@ export default function FontdueAdminToolbar() {
|
|
|
121
122
|
className: "fontdue-admin-toolbar__title"
|
|
122
123
|
}, "Fontdue"), /*#__PURE__*/React.createElement("span", {
|
|
123
124
|
className: "fontdue-admin-toolbar__user"
|
|
124
|
-
},
|
|
125
|
+
}, adminName)), /*#__PURE__*/React.createElement("label", {
|
|
125
126
|
className: "fontdue-admin-toolbar__toggle"
|
|
126
127
|
}, /*#__PURE__*/React.createElement("input", {
|
|
127
128
|
type: "checkbox",
|
|
@@ -13,8 +13,8 @@ import retryImport from '../../retryImport.js';
|
|
|
13
13
|
import useSerializablePreloadedQuery from '../../relay/useSerializablePreloadedQuery.js';
|
|
14
14
|
const ConsentBanner = /*#__PURE__*/lazy(() => retryImport(() => import('../ConsentBanner/index.js')));
|
|
15
15
|
const Tracking = /*#__PURE__*/lazy(() => retryImport(() => import('../Tracking/index.js')));
|
|
16
|
-
// Admin-only; self-gates to
|
|
17
|
-
// it's safe to mount for everyone. It detects
|
|
16
|
+
// Admin-only; self-gates to an admin session and renders nothing otherwise, so
|
|
17
|
+
// it's safe to mount for everyone. It detects admins client-side (storefront
|
|
18
18
|
// renders are sessionless), so it needs no fragment on the provider query.
|
|
19
19
|
// Lazy so it stays out of the provider's main chunk.
|
|
20
20
|
const FontdueAdminToolbar = /*#__PURE__*/lazy(() => retryImport(() => import('../FontdueAdminToolbar/index.js')));
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { SerializablePreloadedQuery } from '../../relay/loadSerializableQuery.js';
|
|
2
|
+
import { SerializablePreloadedQuery, type LoadQueryOptions } from '../../relay/loadSerializableQuery.js';
|
|
3
3
|
import { NewsletterSignupQuery } from '../../__generated__/NewsletterSignupQuery.graphql.js';
|
|
4
4
|
import { Config } from '../ConfigContext.js';
|
|
5
5
|
export type NewsletterSignupPreloadedQuery = SerializablePreloadedQuery<NewsletterSignupQuery>;
|
|
6
|
-
export declare function loadNewsletterSignupQuery(): Promise<NewsletterSignupPreloadedQuery>;
|
|
6
|
+
export declare function loadNewsletterSignupQuery(options?: LoadQueryOptions): Promise<NewsletterSignupPreloadedQuery>;
|
|
7
7
|
export interface NewsletterSignup_props {
|
|
8
8
|
optInLabel?: string;
|
|
9
9
|
successLabel?: string;
|
|
@@ -13,8 +13,8 @@ import useSerializablePreloadedQuery from '../../relay/useSerializablePreloadedQ
|
|
|
13
13
|
import NewsletterSignupQueryNode from '../../__generated__/NewsletterSignupQuery.graphql.js';
|
|
14
14
|
import { EnsureFontdueContext } from '../FontdueContextProvider/index.js';
|
|
15
15
|
const updateCustomerMutation = (_NewsletterSignupUpdateCustomerMutation.hash && _NewsletterSignupUpdateCustomerMutation.hash !== "769087891b6f263122bbb630b3f2ca6c" && console.error("The definition of 'NewsletterSignupUpdateCustomerMutation' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."), _NewsletterSignupUpdateCustomerMutation);
|
|
16
|
-
export async function loadNewsletterSignupQuery() {
|
|
17
|
-
return loadSerializableQuery(NewsletterSignupQueryNode, {});
|
|
16
|
+
export async function loadNewsletterSignupQuery(options) {
|
|
17
|
+
return loadSerializableQuery(NewsletterSignupQueryNode, {}, options);
|
|
18
18
|
}
|
|
19
19
|
const query = (_NewsletterSignupQuery.hash && _NewsletterSignupQuery.hash !== "24b303198a6038318723fc0124548862" && console.error("The definition of 'NewsletterSignupQuery' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."), _NewsletterSignupQuery);
|
|
20
20
|
function NewsletterSignupComponent(_ref) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { TestFontsForm_Query } from '../../__generated__/TestFontsForm_Query.graphql.js';
|
|
3
|
-
import { SerializablePreloadedQuery } from '../../relay/loadSerializableQuery.js';
|
|
3
|
+
import { SerializablePreloadedQuery, type LoadQueryOptions } from '../../relay/loadSerializableQuery.js';
|
|
4
4
|
import { Config } from '../ConfigContext.js';
|
|
5
5
|
export interface TestFontsForm_props {
|
|
6
6
|
agreementLabel?: string;
|
|
@@ -8,7 +8,7 @@ export interface TestFontsForm_props {
|
|
|
8
8
|
newsletterCheckboxChecked?: boolean;
|
|
9
9
|
}
|
|
10
10
|
export type TestFontsFormPreloadedQuery = SerializablePreloadedQuery<TestFontsForm_Query>;
|
|
11
|
-
export declare function loadTestFontsFormQuery(): Promise<TestFontsFormPreloadedQuery>;
|
|
11
|
+
export declare function loadTestFontsFormQuery(options?: LoadQueryOptions): Promise<TestFontsFormPreloadedQuery>;
|
|
12
12
|
export declare function TestFontsFormPreloadedQueryRenderer({ preloadedQuery, ...rest }: TestFontsForm_props & {
|
|
13
13
|
preloadedQuery: TestFontsFormPreloadedQuery;
|
|
14
14
|
}): React.JSX.Element;
|
|
@@ -194,8 +194,8 @@ const TestFontsFormComponent = _ref2 => {
|
|
|
194
194
|
}));
|
|
195
195
|
};
|
|
196
196
|
const query = (_TestFontsForm_Query.hash && _TestFontsForm_Query.hash !== "cd43f0cacc4dcf01cf94fb1ff97197ca" && console.error("The definition of 'TestFontsForm_Query' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."), _TestFontsForm_Query);
|
|
197
|
-
export async function loadTestFontsFormQuery() {
|
|
198
|
-
return loadSerializableQuery(TestFontsFormQueryNode, {});
|
|
197
|
+
export async function loadTestFontsFormQuery(options) {
|
|
198
|
+
return loadSerializableQuery(TestFontsFormQueryNode, {}, options);
|
|
199
199
|
}
|
|
200
200
|
export function TestFontsFormPreloadedQueryRenderer(_ref4) {
|
|
201
201
|
let {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { Alignment, Direction, FeaturesProp } from './types.js';
|
|
3
|
-
import { SerializablePreloadedQuery } from '../../relay/loadSerializableQuery.js';
|
|
3
|
+
import { SerializablePreloadedQuery, type LoadQueryOptions } from '../../relay/loadSerializableQuery.js';
|
|
4
4
|
import { TypeTesterStandaloneQuery } from '../../__generated__/TypeTesterStandaloneQuery.graphql.js';
|
|
5
5
|
import { Config } from '../ConfigContext.js';
|
|
6
6
|
interface TypeTesterStandaloneComponent_props {
|
|
@@ -30,7 +30,7 @@ export interface LoadTypeTesterQueryVariables {
|
|
|
30
30
|
familyName: string;
|
|
31
31
|
styleName: string;
|
|
32
32
|
}
|
|
33
|
-
export declare function loadTypeTesterQuery(variables: LoadTypeTesterQueryVariables): Promise<TypeTesterPreloadedQuery>;
|
|
33
|
+
export declare function loadTypeTesterQuery(variables: LoadTypeTesterQueryVariables, options?: LoadQueryOptions): Promise<TypeTesterPreloadedQuery>;
|
|
34
34
|
export declare function TypeTesterStandalonePreloadedQueryRenderer({ preloadedQuery, ...rest }: TypeTesterStandaloneComponent_props & {
|
|
35
35
|
preloadedQuery: TypeTesterPreloadedQuery;
|
|
36
36
|
}): React.JSX.Element;
|
|
@@ -100,11 +100,11 @@ function TypeTesterStandaloneComponent(_ref) {
|
|
|
100
100
|
}));
|
|
101
101
|
}
|
|
102
102
|
const query = (_TypeTesterStandaloneQuery.hash && _TypeTesterStandaloneQuery.hash !== "951214eacb2370ea3ca0b19bebe7fbe7" && console.error("The definition of 'TypeTesterStandaloneQuery' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."), _TypeTesterStandaloneQuery);
|
|
103
|
-
export async function loadTypeTesterQuery(variables) {
|
|
103
|
+
export async function loadTypeTesterQuery(variables, options) {
|
|
104
104
|
return loadSerializableQuery(TypeTesterStandaloneQueryNode, {
|
|
105
105
|
familyName: variables.familyName,
|
|
106
106
|
styleName: variables.styleName
|
|
107
|
-
});
|
|
107
|
+
}, options);
|
|
108
108
|
}
|
|
109
109
|
export function TypeTesterStandalonePreloadedQueryRenderer(_ref3) {
|
|
110
110
|
let {
|
|
@@ -3,7 +3,7 @@ import { TypeTestersIDQuery } from '../../__generated__/TypeTestersIDQuery.graph
|
|
|
3
3
|
import { TypeTestersSlugQuery } from '../../__generated__/TypeTestersSlugQuery.graphql.js';
|
|
4
4
|
import { Config } from '../ConfigContext.js';
|
|
5
5
|
import { FeaturesProp } from '../TypeTester/types.js';
|
|
6
|
-
import { SerializablePreloadedQuery } from '../../relay/loadSerializableQuery.js';
|
|
6
|
+
import { SerializablePreloadedQuery, type LoadQueryOptions } from '../../relay/loadSerializableQuery.js';
|
|
7
7
|
export interface TypeTesters_props {
|
|
8
8
|
collectionId?: string;
|
|
9
9
|
collectionSlug?: string;
|
|
@@ -33,7 +33,7 @@ export type LoadTypeTestersQueryVariables = ({
|
|
|
33
33
|
tags?: string[] | null;
|
|
34
34
|
excludeTags?: string[] | null;
|
|
35
35
|
};
|
|
36
|
-
export declare function loadTypeTestersQuery(variables: LoadTypeTestersQueryVariables): Promise<TypeTestersPreloadedQuery>;
|
|
36
|
+
export declare function loadTypeTestersQuery(variables: LoadTypeTestersQueryVariables, options?: LoadQueryOptions): Promise<TypeTestersPreloadedQuery>;
|
|
37
37
|
export type TypeTesters_unifiedProps = TypeTesters_props & {
|
|
38
38
|
preloadedQuery?: TypeTestersPreloadedQuery;
|
|
39
39
|
config?: Config;
|
|
@@ -251,7 +251,7 @@ function TypeTestersSlugQueryRenderer(_ref6) {
|
|
|
251
251
|
excludeTags: excludeTags
|
|
252
252
|
}));
|
|
253
253
|
}
|
|
254
|
-
export async function loadTypeTestersQuery(variables) {
|
|
254
|
+
export async function loadTypeTestersQuery(variables, options) {
|
|
255
255
|
const tags = variables.tags ?? null;
|
|
256
256
|
const excludeTags = variables.excludeTags ?? null;
|
|
257
257
|
if (variables.collectionId) {
|
|
@@ -259,14 +259,14 @@ export async function loadTypeTestersQuery(variables) {
|
|
|
259
259
|
collectionId: variables.collectionId,
|
|
260
260
|
tags,
|
|
261
261
|
excludeTags
|
|
262
|
-
});
|
|
262
|
+
}, options);
|
|
263
263
|
}
|
|
264
264
|
if (variables.collectionSlug) {
|
|
265
265
|
return loadSerializableQuery(TypeTestersSlugQueryNode, {
|
|
266
266
|
collectionSlug: variables.collectionSlug,
|
|
267
267
|
tags,
|
|
268
268
|
excludeTags
|
|
269
|
-
});
|
|
269
|
+
}, options);
|
|
270
270
|
}
|
|
271
271
|
throw new Error('loadTypeTestersQuery expected either collectionId or collectionSlug');
|
|
272
272
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type LoadQueryOptions } from './relay/loadSerializableQuery.js';
|
|
1
2
|
import type { FontdueProviderPreloadedQuery } from './components/FontdueProvider/index.js';
|
|
2
3
|
export type { FontdueProviderPreloadedQuery };
|
|
3
|
-
export default function loadFontdueProviderQuery(): Promise<FontdueProviderPreloadedQuery>;
|
|
4
|
+
export default function loadFontdueProviderQuery(options?: LoadQueryOptions): Promise<FontdueProviderPreloadedQuery>;
|
|
@@ -5,6 +5,9 @@ import FontdueProviderQueryNode from './__generated__/FontdueProviderQuery.graph
|
|
|
5
5
|
// `<FontdueProvider preloadedQuery={…}>` in your layout. Without it, aux UI
|
|
6
6
|
// falls back to lazy-fetching on first client render — which causes a visible
|
|
7
7
|
// theming flash because `ThemeConfig` injects CSS variables.
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
//
|
|
9
|
+
// Pass `{ headers: previewAuthHeaders(token) }` (from fontdue-js/preview) to
|
|
10
|
+
// reveal hidden fonts when an admin is in preview mode.
|
|
11
|
+
export default async function loadFontdueProviderQuery(options) {
|
|
12
|
+
return loadSerializableQuery(FontdueProviderQueryNode, {}, options);
|
|
10
13
|
}
|
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
// NEXT_PUBLIC_FONTDUE_IMAGE_HOST is set; see config.ts.
|
|
6
6
|
//
|
|
7
7
|
// NEXT_PUBLIC_FONTDUE_IMAGE_ORIGINS (comma-separated hostnames) should
|
|
8
|
-
// mirror the transformation host's allowed-origins list.
|
|
9
|
-
//
|
|
8
|
+
// mirror the transformation host's allowed-origins list. A leading "*."
|
|
9
|
+
// wildcard matches any subdomain ("*.fontdue.xyz" → "cdn.fontdue.xyz", not
|
|
10
|
+
// the apex or a lookalike); a scheme/port on an entry is ignored. Sources on
|
|
11
|
+
// other hosts — e.g. the /logo endpoint a site serves from its own (possibly
|
|
10
12
|
// customer-owned) domain, which can't be allowlisted — are served as-is
|
|
11
13
|
// rather than as transform URLs Cloudflare would refuse (ERROR 9401).
|
|
12
14
|
//
|
|
@@ -14,6 +16,23 @@
|
|
|
14
16
|
// reads at build time, so it must stay dependency-free, and the variables
|
|
15
17
|
// have to be present when `next build` runs (not just at serve time).
|
|
16
18
|
|
|
19
|
+
// Reduce an allowlist entry to a bare hostname (or "*."-prefixed wildcard),
|
|
20
|
+
// tolerating an optional scheme, path, or port so "https://*.fontdue.xyz"
|
|
21
|
+
// behaves like "*.fontdue.xyz".
|
|
22
|
+
function originHostname(entry) {
|
|
23
|
+
return entry.trim().replace(/^https?:\/\//i, '').split('/')[0].split(':')[0];
|
|
24
|
+
}
|
|
25
|
+
function matchesOrigin(hostname, entry) {
|
|
26
|
+
const origin = originHostname(entry);
|
|
27
|
+
if (!origin) return false;
|
|
28
|
+
if (origin.startsWith('*.')) {
|
|
29
|
+
// ".fontdue.xyz" — endsWith plus the strict length check requires at
|
|
30
|
+
// least one label before the dot, so the apex and lookalikes don't match.
|
|
31
|
+
const suffix = origin.slice(1);
|
|
32
|
+
return hostname.endsWith(suffix) && hostname.length > suffix.length;
|
|
33
|
+
}
|
|
34
|
+
return origin === hostname;
|
|
35
|
+
}
|
|
17
36
|
function transformable(src) {
|
|
18
37
|
const origins = process.env.NEXT_PUBLIC_FONTDUE_IMAGE_ORIGINS;
|
|
19
38
|
if (!origins) return true;
|
|
@@ -23,7 +42,7 @@ function transformable(src) {
|
|
|
23
42
|
} catch {
|
|
24
43
|
return false;
|
|
25
44
|
}
|
|
26
|
-
return origins.split(',').some(
|
|
45
|
+
return origins.split(',').some(entry => matchesOrigin(hostname, entry));
|
|
27
46
|
}
|
|
28
47
|
export default function fontdueImageLoader(_ref) {
|
|
29
48
|
let {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Cookie + endpoint conventions for the admin preview contract.
|
|
2
|
+
//
|
|
3
|
+
// Kept in their own module (no Web/Node APIs) so the client-side toolbar can
|
|
4
|
+
// import them without pulling in the server-only route handler — see ./index.
|
|
5
|
+
|
|
6
|
+
// httpOnly cookie carrying the logged-in admin's short-lived admin token.
|
|
7
|
+
// Only ever read server-side, where it's forwarded as
|
|
8
|
+
// `Authorization: Bearer <token>` so GraphQL reveals hidden (unpublished) fonts.
|
|
9
|
+
export const PREVIEW_TOKEN_COOKIE = 'fontdue_preview_token';
|
|
10
|
+
|
|
11
|
+
// Readable twin of the token cookie. Lets the client toolbar tell preview is
|
|
12
|
+
// on without exposing the token itself.
|
|
13
|
+
export const PREVIEW_MARKER_COOKIE = 'fontdue_preview';
|
|
14
|
+
|
|
15
|
+
// Default path the toolbar POSTs (enter) / DELETEs (exit) to. Relative, so it
|
|
16
|
+
// hits the storefront's own origin. Override per app via
|
|
17
|
+
// FontdueConfig.preview.endpoint (and mount the route to match).
|
|
18
|
+
export const PREVIEW_ENDPOINT = '/api/preview';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PREVIEW_TOKEN_COOKIE, PREVIEW_MARKER_COOKIE, PREVIEW_ENDPOINT } from './constants.js';
|
|
2
|
+
export { PREVIEW_TOKEN_COOKIE, PREVIEW_MARKER_COOKIE, PREVIEW_ENDPOINT };
|
|
3
|
+
export interface PreviewCookieOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Whether to set the cookies' Secure attribute. Defaults to true on https
|
|
6
|
+
* requests and false otherwise, so cookies work over http in local dev.
|
|
7
|
+
*/
|
|
8
|
+
secure?: boolean;
|
|
9
|
+
/** SameSite attribute. Defaults to 'lax' — the toolbar is same-site. */
|
|
10
|
+
sameSite?: 'lax' | 'strict' | 'none';
|
|
11
|
+
/** Cookie path. Defaults to '/'. */
|
|
12
|
+
path?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Web-standard route handler implementing the portable preview contract. Wire
|
|
16
|
+
* it into your framework's route at the path the toolbar targets
|
|
17
|
+
* (`/api/preview` by default):
|
|
18
|
+
*
|
|
19
|
+
* // Astro: src/pages/api/preview.ts
|
|
20
|
+
* import { handlePreviewRequest } from 'fontdue-js/preview';
|
|
21
|
+
* export const ALL: APIRoute = ({ request }) => handlePreviewRequest(request);
|
|
22
|
+
*
|
|
23
|
+
* // React Router 7: app/routes/api.preview.ts
|
|
24
|
+
* import { handlePreviewRequest } from 'fontdue-js/preview';
|
|
25
|
+
* export const action = ({ request }: Route.ActionArgs) =>
|
|
26
|
+
* handlePreviewRequest(request);
|
|
27
|
+
*
|
|
28
|
+
* POST expects a JSON body `{ token: string }`; DELETE takes no body. The token
|
|
29
|
+
* is opaque here — the Fontdue GraphQL server validates it on each request, so
|
|
30
|
+
* a forged token simply reveals nothing. Frameworks that cache HTML should also
|
|
31
|
+
* bypass their shared/CDN cache when PREVIEW_MARKER_COOKIE is present, so a
|
|
32
|
+
* admin preview is never served to the public.
|
|
33
|
+
*/
|
|
34
|
+
export declare function handlePreviewRequest(request: Request, options?: PreviewCookieOptions): Promise<Response>;
|
|
35
|
+
/**
|
|
36
|
+
* Extract the preview token from a Cookie header string, e.g.
|
|
37
|
+
* `readPreviewToken(request.headers.get('cookie'))`. This is the canonical
|
|
38
|
+
* reader — it reverses the encoding `handlePreviewRequest` applies, so the
|
|
39
|
+
* token comes back verbatim. Returns undefined when not in preview.
|
|
40
|
+
*/
|
|
41
|
+
export declare function readPreviewToken(cookieHeader: string | null | undefined): string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Headers to merge into a server-side GraphQL fetch so the request reveals
|
|
44
|
+
* hidden fonts. Spread the result into your app's fetch headers, into
|
|
45
|
+
* fontdue-js's per-render `serverConfig.headers`, or into the `headers` option
|
|
46
|
+
* of a preload helper (loadFontdueProviderQuery, loadTypeTesterQuery, …).
|
|
47
|
+
* Returns {} when there is no token, so it's safe to spread unconditionally.
|
|
48
|
+
*/
|
|
49
|
+
export declare function previewAuthHeaders(token: string | null | undefined): Record<string, string>;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Portable admin-preview contract, shared by every framework adapter.
|
|
2
|
+
//
|
|
3
|
+
// The admin toolbar (FontdueAdminToolbar, auto-mounted by FontdueProvider)
|
|
4
|
+
// detects a logged-in admin session, brokers a short-lived admin token, and
|
|
5
|
+
// POSTs it to a small preview route on the storefront's own origin to "enter
|
|
6
|
+
// preview"; a DELETE exits. In Next.js that route additionally toggles draft
|
|
7
|
+
// mode, but the underlying contract is framework-agnostic:
|
|
8
|
+
//
|
|
9
|
+
// - Enter (POST { token }): set an httpOnly token cookie + a readable marker
|
|
10
|
+
// cookie, so server renders forward the token and the client toolbar can
|
|
11
|
+
// tell preview is on.
|
|
12
|
+
// - Exit (DELETE): clear both cookies.
|
|
13
|
+
// - Server fetch: when the token cookie is present, forward it as
|
|
14
|
+
// `Authorization: Bearer <token>` so GraphQL reveals hidden (unpublished)
|
|
15
|
+
// fonts. The public never has these cookies, so their renders stay
|
|
16
|
+
// sessionless and cacheable.
|
|
17
|
+
//
|
|
18
|
+
// Astro, React Router 7 and TanStack Start all speak the Web Fetch API, so
|
|
19
|
+
// `handlePreviewRequest` is a drop-in route handler for all of them. Next keeps
|
|
20
|
+
// its own route because it layers Next draft mode on top of the same cookies.
|
|
21
|
+
//
|
|
22
|
+
// Why not route this through fontdue-js's per-render `setFontdueServerConfig`
|
|
23
|
+
// store? That store is React.cache-backed and only isolates per request inside
|
|
24
|
+
// an RSC render (Next). Outside RSC — Astro frontmatter, RR7 loaders — writes
|
|
25
|
+
// are no-ops (see relay/serverConfig.ts). Two portable ways to get the token to
|
|
26
|
+
// server fetches instead:
|
|
27
|
+
//
|
|
28
|
+
// - Ambient (recommended): wrap requests in `runWithPreview` from
|
|
29
|
+
// fontdue-js/preview/server, which holds the token in AsyncLocalStorage for
|
|
30
|
+
// the request so every fontdue-js fetch/preload forwards it with no
|
|
31
|
+
// per-call plumbing — and forces preview responses out of shared caches.
|
|
32
|
+
// - Explicit: read the cookie per request and pass `previewAuthHeaders(token)`
|
|
33
|
+
// via the `headers` option on the app's own GraphQL fetch and on each
|
|
34
|
+
// preload helper (loadFontdueProviderQuery, loadTypeTesterQuery, …). This
|
|
35
|
+
// mirrors how headless-CMS previews (e.g. Sanity's loadQuery) pass
|
|
36
|
+
// per-request state explicitly, and is the fallback where ambient context
|
|
37
|
+
// can't propagate.
|
|
38
|
+
|
|
39
|
+
import { PREVIEW_TOKEN_COOKIE, PREVIEW_MARKER_COOKIE, PREVIEW_ENDPOINT } from './constants.js';
|
|
40
|
+
export { PREVIEW_TOKEN_COOKIE, PREVIEW_MARKER_COOKIE, PREVIEW_ENDPOINT };
|
|
41
|
+
function serializeCookie(name, value, opts) {
|
|
42
|
+
const sameSite = opts.sameSite === 'strict' ? 'Strict' : opts.sameSite === 'none' ? 'None' : 'Lax';
|
|
43
|
+
const parts = [`${name}=${encodeURIComponent(value)}`, `Path=${opts.path ?? '/'}`, `SameSite=${sameSite}`];
|
|
44
|
+
if (opts.httpOnly) parts.push('HttpOnly');
|
|
45
|
+
if (opts.secure) parts.push('Secure');
|
|
46
|
+
if (opts.maxAge != null) parts.push(`Max-Age=${opts.maxAge}`);
|
|
47
|
+
return parts.join('; ');
|
|
48
|
+
}
|
|
49
|
+
function isSecureRequest(request) {
|
|
50
|
+
try {
|
|
51
|
+
return new URL(request.url).protocol === 'https:';
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function jsonResponse(body, status) {
|
|
57
|
+
let setCookies = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
58
|
+
const headers = new Headers({
|
|
59
|
+
'content-type': 'application/json'
|
|
60
|
+
});
|
|
61
|
+
for (const cookie of setCookies) headers.append('Set-Cookie', cookie);
|
|
62
|
+
return new Response(JSON.stringify(body), {
|
|
63
|
+
status,
|
|
64
|
+
headers
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Web-standard route handler implementing the portable preview contract. Wire
|
|
70
|
+
* it into your framework's route at the path the toolbar targets
|
|
71
|
+
* (`/api/preview` by default):
|
|
72
|
+
*
|
|
73
|
+
* // Astro: src/pages/api/preview.ts
|
|
74
|
+
* import { handlePreviewRequest } from 'fontdue-js/preview';
|
|
75
|
+
* export const ALL: APIRoute = ({ request }) => handlePreviewRequest(request);
|
|
76
|
+
*
|
|
77
|
+
* // React Router 7: app/routes/api.preview.ts
|
|
78
|
+
* import { handlePreviewRequest } from 'fontdue-js/preview';
|
|
79
|
+
* export const action = ({ request }: Route.ActionArgs) =>
|
|
80
|
+
* handlePreviewRequest(request);
|
|
81
|
+
*
|
|
82
|
+
* POST expects a JSON body `{ token: string }`; DELETE takes no body. The token
|
|
83
|
+
* is opaque here — the Fontdue GraphQL server validates it on each request, so
|
|
84
|
+
* a forged token simply reveals nothing. Frameworks that cache HTML should also
|
|
85
|
+
* bypass their shared/CDN cache when PREVIEW_MARKER_COOKIE is present, so a
|
|
86
|
+
* admin preview is never served to the public.
|
|
87
|
+
*/
|
|
88
|
+
export async function handlePreviewRequest(request) {
|
|
89
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
90
|
+
const base = {
|
|
91
|
+
secure: options.secure ?? isSecureRequest(request),
|
|
92
|
+
sameSite: options.sameSite ?? 'lax',
|
|
93
|
+
path: options.path ?? '/'
|
|
94
|
+
};
|
|
95
|
+
if (request.method === 'DELETE') {
|
|
96
|
+
return jsonResponse({
|
|
97
|
+
ok: true,
|
|
98
|
+
preview: false
|
|
99
|
+
}, 200, [serializeCookie(PREVIEW_TOKEN_COOKIE, '', {
|
|
100
|
+
...base,
|
|
101
|
+
httpOnly: true,
|
|
102
|
+
maxAge: 0
|
|
103
|
+
}), serializeCookie(PREVIEW_MARKER_COOKIE, '', {
|
|
104
|
+
...base,
|
|
105
|
+
httpOnly: false,
|
|
106
|
+
maxAge: 0
|
|
107
|
+
})]);
|
|
108
|
+
}
|
|
109
|
+
if (request.method === 'POST') {
|
|
110
|
+
const body = await request.json().catch(() => ({}));
|
|
111
|
+
const token = body.token;
|
|
112
|
+
if (typeof token !== 'string' || token === '') {
|
|
113
|
+
return jsonResponse({
|
|
114
|
+
ok: false,
|
|
115
|
+
error: 'Missing preview token'
|
|
116
|
+
}, 400);
|
|
117
|
+
}
|
|
118
|
+
return jsonResponse({
|
|
119
|
+
ok: true,
|
|
120
|
+
preview: true
|
|
121
|
+
}, 200, [serializeCookie(PREVIEW_TOKEN_COOKIE, token, {
|
|
122
|
+
...base,
|
|
123
|
+
httpOnly: true
|
|
124
|
+
}), serializeCookie(PREVIEW_MARKER_COOKIE, '1', {
|
|
125
|
+
...base,
|
|
126
|
+
httpOnly: false
|
|
127
|
+
})]);
|
|
128
|
+
}
|
|
129
|
+
return jsonResponse({
|
|
130
|
+
ok: false,
|
|
131
|
+
error: 'Method not allowed'
|
|
132
|
+
}, 405);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Extract the preview token from a Cookie header string, e.g.
|
|
137
|
+
* `readPreviewToken(request.headers.get('cookie'))`. This is the canonical
|
|
138
|
+
* reader — it reverses the encoding `handlePreviewRequest` applies, so the
|
|
139
|
+
* token comes back verbatim. Returns undefined when not in preview.
|
|
140
|
+
*/
|
|
141
|
+
export function readPreviewToken(cookieHeader) {
|
|
142
|
+
if (!cookieHeader) return undefined;
|
|
143
|
+
for (const part of cookieHeader.split(';')) {
|
|
144
|
+
const idx = part.indexOf('=');
|
|
145
|
+
if (idx === -1) continue;
|
|
146
|
+
if (part.slice(0, idx).trim() !== PREVIEW_TOKEN_COOKIE) continue;
|
|
147
|
+
const value = part.slice(idx + 1).trim();
|
|
148
|
+
return value === '' ? undefined : decodeURIComponent(value);
|
|
149
|
+
}
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Headers to merge into a server-side GraphQL fetch so the request reveals
|
|
155
|
+
* hidden fonts. Spread the result into your app's fetch headers, into
|
|
156
|
+
* fontdue-js's per-render `serverConfig.headers`, or into the `headers` option
|
|
157
|
+
* of a preload helper (loadFontdueProviderQuery, loadTypeTesterQuery, …).
|
|
158
|
+
* Returns {} when there is no token, so it's safe to spread unconditionally.
|
|
159
|
+
*/
|
|
160
|
+
export function previewAuthHeaders(token) {
|
|
161
|
+
return token ? {
|
|
162
|
+
authorization: `Bearer ${token}`
|
|
163
|
+
} : {};
|
|
164
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Whether the current async context is an admin preview. */
|
|
2
|
+
export declare function isPreviewing(): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Preview auth headers for the current async context, or {} when not previewing.
|
|
5
|
+
* Spread into a hand-rolled fetch you make inside `runWithPreview`; the
|
|
6
|
+
* fontdue-js fetch/preload helpers pick these up automatically and don't need
|
|
7
|
+
* it.
|
|
8
|
+
*/
|
|
9
|
+
export declare function ambientPreviewHeaders(): Record<string, string>;
|
|
10
|
+
/**
|
|
11
|
+
* Run `next` (your framework's render/loader chain) with the request's preview
|
|
12
|
+
* token in ambient context, then return its Response. When the request is
|
|
13
|
+
* previewing, the response's cache headers are rewritten so it is never stored
|
|
14
|
+
* in a shared/CDN cache. Public (no-token) requests pass through untouched and
|
|
15
|
+
* stay fully cacheable.
|
|
16
|
+
*
|
|
17
|
+
* `next` is your middleware's continuation — `next` in Astro, `next` in a React
|
|
18
|
+
* Router 7 middleware. It must return (a promise of) the Response.
|
|
19
|
+
*/
|
|
20
|
+
export declare function runWithPreview(request: Request, next: () => Response | Promise<Response>): Promise<Response>;
|