@ampless/runtime 0.2.0-alpha.2 → 0.2.0-alpha.4
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/dispatchers/index.d.ts +22 -9
- package/dist/dispatchers/index.js +6 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +14 -1
- package/dist/middleware.d.ts +7 -1
- package/dist/middleware.js +2 -8
- package/dist/routes/index.d.ts +7 -4
- package/dist/routes/index.js +3 -0
- package/package.json +3 -3
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
1
2
|
import { Metadata } from 'next';
|
|
2
3
|
import { Ampless } from '../index.js';
|
|
3
4
|
import 'ampless';
|
|
@@ -7,16 +8,12 @@ interface Props$2 {
|
|
|
7
8
|
siteId: string;
|
|
8
9
|
}>;
|
|
9
10
|
}
|
|
10
|
-
type ThemeHomeDispatcher = (props: Props$2) => Promise<
|
|
11
|
+
type ThemeHomeDispatcher = (props: Props$2) => Promise<ReactNode>;
|
|
11
12
|
type ThemeHomeMetadata = (props: Props$2) => Promise<Metadata>;
|
|
12
13
|
/**
|
|
13
14
|
* Home page dispatcher. Resolves the active theme for the request's
|
|
14
15
|
* siteId and renders the theme's `components.Home` server component
|
|
15
16
|
* with the same `params` Promise it was passed.
|
|
16
|
-
*
|
|
17
|
-
* The return type is `unknown` (cast at the route boundary) because
|
|
18
|
-
* Next.js page-component prop types vary by route shape and the
|
|
19
|
-
* underlying theme components are arbitrary server components.
|
|
20
17
|
*/
|
|
21
18
|
declare function createThemeHomeDispatcher(ampless: Ampless): ThemeHomeDispatcher;
|
|
22
19
|
/** generateMetadata factory for the home dispatcher. */
|
|
@@ -28,12 +25,28 @@ interface Props$1 {
|
|
|
28
25
|
slug: string;
|
|
29
26
|
}>;
|
|
30
27
|
}
|
|
31
|
-
type ThemePostDispatcher = (props: Props$1) => Promise<
|
|
28
|
+
type ThemePostDispatcher = (props: Props$1) => Promise<ReactNode>;
|
|
32
29
|
type ThemePostMetadata = (props: Props$1) => Promise<Metadata>;
|
|
33
30
|
/**
|
|
34
31
|
* Post page dispatcher. Resolves the active theme and renders the
|
|
35
|
-
* theme's `components.Post` server component.
|
|
36
|
-
*
|
|
32
|
+
* theme's `components.Post` server component.
|
|
33
|
+
*
|
|
34
|
+
* Before delegating, the dispatcher peeks at the post's metadata: if
|
|
35
|
+
* `metadata.no_layout === true`, the post is meant to be served as
|
|
36
|
+
* bare HTML (own DOCTYPE, no theme chrome, no Next.js root layout).
|
|
37
|
+
* Next.js page.tsx can't bypass the root layout, so we 308-redirect
|
|
38
|
+
* to the raw route handler at `/raw/<slug>` which returns the body
|
|
39
|
+
* unchanged. The redirect is permanent because no_layout is a
|
|
40
|
+
* persistent property of the post — bookmarks naturally settle on
|
|
41
|
+
* the `/raw/` URL.
|
|
42
|
+
*
|
|
43
|
+
* The metadata peek is an extra AppSync call before theme resolve,
|
|
44
|
+
* but it's the same query the theme's Post component would make
|
|
45
|
+
* anyway; AppSync's query-level dedupe within a single request keeps
|
|
46
|
+
* the wire cost flat in practice.
|
|
47
|
+
*
|
|
48
|
+
* If the theme doesn't declare a Post component at all, returns
|
|
49
|
+
* Next.js's notFound() (404).
|
|
37
50
|
*/
|
|
38
51
|
declare function createThemePostDispatcher(ampless: Ampless): ThemePostDispatcher;
|
|
39
52
|
/** generateMetadata factory for the post dispatcher. */
|
|
@@ -45,7 +58,7 @@ interface Props {
|
|
|
45
58
|
tag: string;
|
|
46
59
|
}>;
|
|
47
60
|
}
|
|
48
|
-
type ThemeTagDispatcher = (props: Props) => Promise<
|
|
61
|
+
type ThemeTagDispatcher = (props: Props) => Promise<ReactNode>;
|
|
49
62
|
type ThemeTagMetadata = (props: Props) => Promise<Metadata>;
|
|
50
63
|
/**
|
|
51
64
|
* Tag page dispatcher. Resolves the active theme and renders the
|
|
@@ -17,10 +17,14 @@ function createThemeHomeMetadata(ampless) {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
// src/dispatchers/post.ts
|
|
20
|
-
import { notFound } from "next/navigation";
|
|
20
|
+
import { notFound, redirect } from "next/navigation";
|
|
21
21
|
function createThemePostDispatcher(ampless) {
|
|
22
22
|
return async function SitePostDispatcher({ params }) {
|
|
23
|
-
const { siteId } = await params;
|
|
23
|
+
const { siteId, slug } = await params;
|
|
24
|
+
const post = await ampless.getPublishedPost(slug, { siteId });
|
|
25
|
+
if (post?.metadata?.no_layout === true) {
|
|
26
|
+
redirect(`/raw/${slug}`);
|
|
27
|
+
}
|
|
24
28
|
const { module } = await ampless.resolveActiveTheme(siteId);
|
|
25
29
|
const Post = module.components.Post;
|
|
26
30
|
if (!Post) notFound();
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -28,6 +28,18 @@ function decodeBody(value) {
|
|
|
28
28
|
return value;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
function decodeMetadata(value) {
|
|
32
|
+
if (value === null || value === void 0) return void 0;
|
|
33
|
+
const parsed = typeof value === "string" ? safeJsonParse(value) : value;
|
|
34
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : void 0;
|
|
35
|
+
}
|
|
36
|
+
function safeJsonParse(value) {
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(value);
|
|
39
|
+
} catch {
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
31
43
|
function toCorePost(p) {
|
|
32
44
|
return {
|
|
33
45
|
postId: p.postId,
|
|
@@ -39,7 +51,8 @@ function toCorePost(p) {
|
|
|
39
51
|
body: decodeBody(p.body),
|
|
40
52
|
status: p.status ?? "draft",
|
|
41
53
|
publishedAt: p.publishedAt ?? void 0,
|
|
42
|
-
tags: (p.tags ?? []).filter((t) => typeof t === "string")
|
|
54
|
+
tags: (p.tags ?? []).filter((t) => typeof t === "string"),
|
|
55
|
+
metadata: decodeMetadata(p.metadata)
|
|
43
56
|
};
|
|
44
57
|
}
|
|
45
58
|
function createPostsApi(outputs) {
|
package/dist/middleware.d.ts
CHANGED
|
@@ -10,12 +10,18 @@ type MiddlewareFn = (request: NextRequest) => NextResponse;
|
|
|
10
10
|
*
|
|
11
11
|
* - hostname → siteId resolution (multi-site rewrites)
|
|
12
12
|
* - `/path` → `/site/<siteId>/path` internal rewrite
|
|
13
|
-
* - `<slug>.html` → `/site/<siteId>/raw/<slug>.html` (bare HTML route)
|
|
14
13
|
* - `?previewTheme=<name>` → `x-preview-theme` header forwarding
|
|
15
14
|
* - `Cache-Control: private, no-store` in multi-site mode (Amplify
|
|
16
15
|
* Hosting's CloudFront cache key doesn't include Host, so SSR
|
|
17
16
|
* responses would cross-contaminate at the same path)
|
|
18
17
|
*
|
|
18
|
+
* No-layout / bare-HTML routing is data-driven: the theme post
|
|
19
|
+
* dispatcher reads `post.metadata.no_layout` and redirects to
|
|
20
|
+
* `/raw/<slug>` when set. The plain prefix-prepend below picks up
|
|
21
|
+
* that path because the raw route lives at
|
|
22
|
+
* `app/site/<siteId>/raw/<slug>/route.ts` — no special middleware
|
|
23
|
+
* branch is needed.
|
|
24
|
+
*
|
|
19
25
|
* The factory captures the multi-site flag at construction time so the
|
|
20
26
|
* hot path stays a pair of cheap header lookups.
|
|
21
27
|
*/
|
package/dist/middleware.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// src/middleware.ts
|
|
2
2
|
import { NextResponse } from "next/server";
|
|
3
3
|
import { resolveSiteId, isMultiSite } from "ampless";
|
|
4
|
-
var RAW_HTML_PATH_RE = /^\/([^/]+\.html)$/;
|
|
5
4
|
function createAmplessMiddleware({ cmsConfig }) {
|
|
6
5
|
const MULTI_SITE = isMultiSite(cmsConfig);
|
|
7
6
|
return function middleware(request) {
|
|
@@ -12,13 +11,8 @@ function createAmplessMiddleware({ cmsConfig }) {
|
|
|
12
11
|
}
|
|
13
12
|
const url = request.nextUrl.clone();
|
|
14
13
|
if (!url.pathname.startsWith("/site/")) {
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
url.pathname = `/site/${siteId}/raw/${rawMatch[1]}`;
|
|
18
|
-
} else {
|
|
19
|
-
const tail = url.pathname === "/" ? "" : url.pathname;
|
|
20
|
-
url.pathname = `/site/${siteId}${tail}`;
|
|
21
|
-
}
|
|
14
|
+
const tail = url.pathname === "/" ? "" : url.pathname;
|
|
15
|
+
url.pathname = `/site/${siteId}${tail}`;
|
|
22
16
|
}
|
|
23
17
|
const previewTheme = url.searchParams.get("previewTheme");
|
|
24
18
|
const requestHeaders = new Headers(request.headers);
|
package/dist/routes/index.d.ts
CHANGED
|
@@ -48,10 +48,13 @@ type RawRouteHandler = (req: Request, ctx: Ctx) => Promise<Response>;
|
|
|
48
48
|
* Bare HTML route. Returns the published post's rendered body as the
|
|
49
49
|
* entire HTTP response — no Next.js root layout, no theme chrome.
|
|
50
50
|
*
|
|
51
|
-
* Reached via the
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
51
|
+
* Reached via the data-driven dispatcher path: the theme post page
|
|
52
|
+
* checks `post.metadata.no_layout` and redirects to `/raw/<slug>` when
|
|
53
|
+
* set. Posts without that flag never hit this route in practice; the
|
|
54
|
+
* handler additionally enforces the same check here so a direct
|
|
55
|
+
* `/raw/<slug>` request can't bypass the theme for a post that wasn't
|
|
56
|
+
* marked no-layout (would otherwise surface the body without any
|
|
57
|
+
* chrome the editor didn't intend).
|
|
55
58
|
*
|
|
56
59
|
* Why this is a route handler (not a page): Next.js's root layout
|
|
57
60
|
* always emits `<html>` / `<head>` / `<body>`, which means a normal
|
package/dist/routes/index.js
CHANGED
|
@@ -75,6 +75,9 @@ function createRawRouteHandler(ampless) {
|
|
|
75
75
|
if (!post) {
|
|
76
76
|
return new Response("Not Found", { status: 404 });
|
|
77
77
|
}
|
|
78
|
+
if (post.metadata?.no_layout !== true) {
|
|
79
|
+
return new Response("Not Found", { status: 404 });
|
|
80
|
+
}
|
|
78
81
|
return new Response(ampless.renderBody(post), {
|
|
79
82
|
status: 200,
|
|
80
83
|
headers: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ampless/runtime",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.4",
|
|
4
4
|
"description": "Public-side runtime for ampless: post fetching, theme dispatch, middleware, public route handlers",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"clsx": "^2.1.1",
|
|
49
49
|
"lucide-react": "^1.16.0",
|
|
50
50
|
"tailwind-merge": "^3.6.0",
|
|
51
|
-
"ampless": "0.2.0-alpha.
|
|
52
|
-
"@ampless/plugin-og-image": "0.2.0-alpha.
|
|
51
|
+
"ampless": "0.2.0-alpha.2",
|
|
52
|
+
"@ampless/plugin-og-image": "0.2.0-alpha.2"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"next": "^15 || ^16",
|