@panomc/theme-core 1.2.0-dev.1 → 1.2.1-dev.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panomc/theme-core",
3
- "version": "1.2.0-dev.1",
3
+ "version": "1.2.1-dev.1",
4
4
  "description": "The Pano theme engine: hooks factories, plugin engine, headless page controllers, default views and the theme CLI. Themes depend on this package instead of forking vanilla-theme.",
5
5
  "author": "PanoMC",
6
6
  "pano": {
@@ -120,6 +120,35 @@ describe("resolveHome", () => {
120
120
  expect(messages).toHaveLength(1);
121
121
  });
122
122
 
123
+ test("a page of the theme itself (no plugin page there) is a redirect to it", () => {
124
+ const { messages, warn } = warnings();
125
+ const themeRoutes = ["/support", "/player/[player]", "/tickets"];
126
+ const options = { warn, homePages: HOME_PAGES, themeRoutes };
127
+
128
+ expect(resolveHome({}, "custom:/support", PAGES, options)).toEqual({ kind: "redirect", path: "/support" });
129
+ expect(resolveHome(THEME, "custom:/player/ahmet?tab=1", PAGES, options)).toEqual({ kind: "redirect", path: "/player/ahmet" });
130
+ // a plugin page at the same path still wins, and a path the theme does not have still falls back
131
+ expect(resolveHome(THEME, "custom:/rules", PAGES, { ...options, themeRoutes: ["/rules"] })).toEqual({ kind: "path", path: "/rules" });
132
+ expect(resolveHome(THEME, "custom:/missing", PAGES, options)).toEqual({ kind: "page", id: "landing" });
133
+ expect(messages).toHaveLength(1);
134
+
135
+ // a renamed core page: the admin types the public path, the theme's routes are canonical
136
+ routes.setRouteConfig({ routes: { rename: { "/support": "/help" } } });
137
+ expect(resolveHome({}, "custom:/help", PAGES, options)).toEqual({ kind: "redirect", path: "/help" });
138
+ });
139
+
140
+ test("routePatternOf: canonical patterns of the theme's pages, without the catch-all and engine pages", async () => {
141
+ const { routePatternOf } = await import("../theme-routes.js");
142
+
143
+ expect(routePatternOf("/src/routes/(theme)/rules/+page.svelte")).toBe("/rules");
144
+ expect(routePatternOf("/src/routes/(theme)/player/[player]/+page.svelte")).toBe("/player/[player]");
145
+ expect(routePatternOf("/src/routes/(theme-settings)/theme-settings/+page.svelte")).toBe("/theme-settings");
146
+ expect(routePatternOf("/src/routes/(theme)/+page.svelte")).toBeNull();
147
+ expect(routePatternOf("/src/routes/(theme)/posts/+page.svelte")).toBeNull();
148
+ expect(routePatternOf("/src/routes/(theme)/__pano/views/+page.svelte")).toBeNull();
149
+ expect(routePatternOf("/src/routes/(plugin-ui)/[...path]/+page.svelte")).toBeNull();
150
+ });
151
+
123
152
  test("a pattern instead of a concrete path: the default, one warning (/store/vip is fine)", () => {
124
153
  const { messages, warn } = warnings();
125
154
 
package/src/lib/home.js CHANGED
@@ -8,15 +8,20 @@
8
8
  // { kind: "posts" } the posts feed (today's page)
9
9
  // { kind: "page", id } a page of the theme (`home.options.<id>.page`)
10
10
  // { kind: "path", path } the plugin page registered at a concrete canonical path
11
+ // { kind: "redirect", path } a page of the theme itself (`/rules`, `/support`, a `routes.add` page): `/`
12
+ // sends the visitor there (302), the page is not rendered at `/`
11
13
  //
12
14
  // A choice that cannot be shown falls back to `home.default`, then to the posts feed, with one
13
15
  // console.warn each. The posts feed is always served at `/posts` as well.
14
16
  import { findMatch } from "@panomc/sdk/core/js/RouteMatcher.js";
15
17
 
16
18
  import { getThemeConfig, getThemeMeta } from "../registry/index.js";
17
- import { route } from "../registry/routes.js";
19
+ import { resolveCanonical, route } from "../registry/routes.js";
18
20
 
19
- /** @typedef {{ kind: "posts" } | { kind: "page", id: string } | { kind: "path", path: string }} Home */
21
+ /**
22
+ * @typedef {{ kind: "posts" } | { kind: "page", id: string } | { kind: "path", path: string }
23
+ * | { kind: "redirect", path: string }} Home
24
+ */
20
25
 
21
26
  const POSTS_PATH = "/posts";
22
27
  const CUSTOM_PREFIX = "custom:";
@@ -32,13 +37,17 @@ const found = (home) => ({ home });
32
37
  * @param {string | null | undefined} homePage `siteInfo.homePage`
33
38
  * @param {Record<string, any> | null | undefined} registeredPages the plugin pages by path; `null` / `undefined`
34
39
  * = not known here: a path is then taken as it is, without looking for the page
35
- * @param {{ homePages?: Record<string, unknown>, warn?: (message: string) => void }} [options]
40
+ * @param {{ homePages?: Record<string, unknown>, themeRoutes?: string[], warn?: (message: string) => void }} [options]
36
41
  * `homePages` = the page options the build found (a `page` option without a file is refused);
42
+ * `themeRoutes` = the canonical route patterns of the theme's own pages (`/rules`, `/player/[player]`): a path
43
+ * that no plugin registered but that is one of them resolves to `{ kind: "redirect" }`;
37
44
  * `warn` = how a fallback is reported (default `console.warn`)
38
45
  * @returns {Home}
39
46
  */
40
47
  export function resolveHome(themeConfig, homePage, registeredPages, options = {}) {
41
- const { homePages, warn = console.warn } = options;
48
+ const { homePages, themeRoutes = [], warn = console.warn } = options;
49
+ /** @type {Record<string, {}>} */
50
+ const themePages = Object.fromEntries(themeRoutes.map((pattern) => [pattern, {}]));
42
51
  const home = themeConfig?.home && typeof themeConfig.home === "object" ? themeConfig.home : null;
43
52
  const configured = home !== null;
44
53
  const themeOptions = configured && home.options && typeof home.options === "object" ? home.options : {};
@@ -66,6 +75,14 @@ export function resolveHome(themeConfig, homePage, registeredPages, options = {}
66
75
  const page = findMatch(registeredPages, clean);
67
76
 
68
77
  if (!page) {
78
+ // Not a plugin page: a page of the theme itself is shown at its own address (the admin typed its public
79
+ // path; the theme's routes are canonical, so a renamed one is looked up by its canonical path).
80
+ const canonical = resolveCanonical(clean);
81
+
82
+ if (canonical && canonical !== "/" && canonical !== POSTS_PATH && findMatch(themePages, canonical)) {
83
+ return found({ kind: "redirect", path: clean });
84
+ }
85
+
69
86
  return problem(`no page is registered at "${clean}" (is its plugin installed and on?)`);
70
87
  }
71
88
 
@@ -162,9 +179,10 @@ export function isPostsRoute(event) {
162
179
  * Waits for the layout first: the plugin pages are registered by then.
163
180
  * @param {any} event SvelteKit load event
164
181
  * @param {() => Record<string, any>} getRegisteredPages the current plugin pages (the table is replaced on a rebuild)
182
+ * @param {string[]} [themeRoutes] the theme's own page patterns (`getThemeRoutePatterns()`)
165
183
  * @returns {Promise<Home>}
166
184
  */
167
- export async function resolveHomeFor(event, getRegisteredPages) {
185
+ export async function resolveHomeFor(event, getRegisteredPages, themeRoutes = []) {
168
186
  if (isPostsRoute(event)) {
169
187
  return { kind: "posts" };
170
188
  }
@@ -173,6 +191,7 @@ export async function resolveHomeFor(event, getRegisteredPages) {
173
191
 
174
192
  return resolveHome(getThemeConfig(), parentData?.session?.siteInfo?.homePage ?? null, getRegisteredPages(), {
175
193
  homePages: getThemeMeta()?.homePages,
194
+ themeRoutes,
176
195
  });
177
196
  }
178
197
 
@@ -11,17 +11,26 @@
11
11
  import { loadView } from "$pano/registry/index.js";
12
12
  import { registeredPages, findMatch } from "$pano/lib/PluginManager.js";
13
13
  import { hasPermission } from "$pano/lib/auth.util.js";
14
+ import { redirect } from "@sveltejs/kit";
15
+
14
16
  import { resolveHomeFor } from "$pano/lib/home.js";
17
+ import { getThemeRoutePatterns } from "$pano/lib/theme-routes.js";
15
18
  import { loadHomeTarget } from "$pano/routes/plugin-ui/load.js";
16
19
  import Page, { sidebarDeps } from "$pano/routes/plugin-ui/Page.svelte";
17
20
 
18
21
  /**
19
22
  * `/` shows what the admin picked as the home page (the posts feed unless a plugin page or a page of the
20
- * theme was picked); `/posts` is the same controller and always shows the feed (doc 01 section 9).
23
+ * theme was picked; a core page such as `/rules` is a redirect to it); `/posts` is the same controller and
24
+ * always shows the feed (doc 01 section 9).
21
25
  * @type {import('@sveltejs/kit').PageLoad}
22
26
  */
23
27
  export async function load(event) {
24
- const home = await resolveHomeFor(event, () => registeredPages);
28
+ const home = await resolveHomeFor(event, () => registeredPages, getThemeRoutePatterns());
29
+
30
+ // A page of the theme itself (`custom:/rules`): `/` sends the visitor to it.
31
+ if (home.kind === "redirect") {
32
+ throw redirect(302, home.path + event.url.search);
33
+ }
25
34
 
26
35
  if (home.kind === "page" || home.kind === "path") {
27
36
  const target = await loadHomeTarget(event, home, {
@@ -0,0 +1,56 @@
1
+ // The pages of the theme itself (`src/routes`): the core routes `theme-core sync` writes (`/rules`, `/support`,
2
+ // `/player/[player]`, ...) and the theme's own `routes.add` pages, as canonical route patterns. The home page
3
+ // setting uses them: a custom path that is one of these pages is a page of the site even though no plugin
4
+ // registered it (doc 01 section 9).
5
+
6
+ /** Route patterns that are never a home page target: `/` itself, the posts feed and the engine's own pages. */
7
+ const EXCLUDED = new Set(["/", "/posts"]);
8
+
9
+ /**
10
+ * `/src/routes/(theme)/player/[player]/+page.svelte` -> `/player/[player]`; `null` for the plugin catch-all
11
+ * (`(plugin-ui)`, which matches every path) and the engine's internal pages (`/__pano...`).
12
+ * @param {string} file
13
+ * @returns {string | null}
14
+ */
15
+ export function routePatternOf(file) {
16
+ const id = file.replace(/^.*?\/src\/routes/, "").replace(/\/\+page\.svelte$/, "");
17
+
18
+ if (id.includes("(plugin-ui)")) {
19
+ return null;
20
+ }
21
+
22
+ const pattern = id.replace(/\/\([^/)]*\)/g, "") || "/";
23
+
24
+ if (pattern.startsWith("/__pano") || EXCLUDED.has(pattern)) {
25
+ return null;
26
+ }
27
+
28
+ return pattern;
29
+ }
30
+
31
+ /** @type {string[] | null} */
32
+ let cached = null;
33
+
34
+ /**
35
+ * The theme's page patterns. Empty outside a Vite build (the engine's own `bun test` runs have no `src/routes`).
36
+ * @returns {string[]}
37
+ */
38
+ export function getThemeRoutePatterns() {
39
+ if (cached) {
40
+ return cached;
41
+ }
42
+
43
+ /** @type {Record<string, unknown>} */
44
+ let files = {};
45
+
46
+ try {
47
+ // Vite replaces this call with the file list at build time; elsewhere `import.meta.glob` is not a function.
48
+ files = import.meta.glob("/src/routes/**/+page.svelte");
49
+ } catch {
50
+ files = {};
51
+ }
52
+
53
+ cached = [...new Set(Object.keys(files).map(routePatternOf).filter((pattern) => pattern !== null))].sort();
54
+
55
+ return cached;
56
+ }