soames-astro-theme 0.1.0 → 0.1.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,7 +1,7 @@
1
1
  {
2
2
  "name": "soames-astro-theme",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "description": "Shared Astro theme for the Soames ecosystem (WordPress headless + React islands). Successor to soames-gatsby-theme.",
6
6
  "keywords": [
7
7
  "astro",
@@ -23,7 +23,19 @@ export async function getStaticPaths() {
23
23
  });
24
24
  }
25
25
 
26
- const { page } = Astro.props;
26
+ let { page } = Astro.props;
27
+
28
+ // Dev only: getStaticPaths (and the WP content it bakes into props) is cached for
29
+ // the life of `astro dev`, so edits in WordPress wouldn't show until a restart.
30
+ // Re-fetch the current page on each request in dev so a browser refresh reflects
31
+ // content changes. Tree-shaken out of production builds (import.meta.env.DEV).
32
+ if (import.meta.env.DEV) {
33
+ const clean = (Astro.params.uri ?? "").replace(/^\/+|\/+$/g, "");
34
+ const fresh = (await getPages()).find(
35
+ (p) => (p.uri ?? "").replace(/^\/+|\/+$/g, "") === clean
36
+ );
37
+ if (fresh) page = fresh;
38
+ }
27
39
  ---
28
40
  <Base pageTitle={page.title} description={page.excerpt}>
29
41
  <HeroHeader
@@ -36,8 +36,29 @@ export async function getStaticPaths() {
36
36
  });
37
37
  }
38
38
 
39
- const { posts, pageNum, totalPages, prevPath, nextPath, heroTitle, heroBg, heroOverlay } =
39
+ let { posts, pageNum, totalPages, prevPath, nextPath, heroTitle, heroBg, heroOverlay } =
40
40
  Astro.props;
41
+
42
+ // Dev only: refresh this archive page's data on each request (getStaticPaths is
43
+ // cached for the life of `astro dev`, so WP edits wouldn't show until a restart).
44
+ // Recomputes the same slice/hero as getStaticPaths for the current pageNum.
45
+ // Removed from production builds (import.meta.env.DEV).
46
+ if (import.meta.env.DEV) {
47
+ const [all, perPage, postsPage] = await Promise.all([
48
+ getPosts(),
49
+ getPostsPerPage(),
50
+ getPostsPage(),
51
+ ]);
52
+ const base = `/${postsPage?.slug || "blog"}`;
53
+ totalPages = Math.max(1, Math.ceil(all.length / perPage));
54
+ posts = all.slice((pageNum - 1) * perPage, pageNum * perPage);
55
+ prevPath = pageNum > 1 ? (pageNum === 2 ? `${base}/` : `${base}/${pageNum - 1}/`) : null;
56
+ nextPath = pageNum < totalPages ? `${base}/${pageNum + 1}/` : null;
57
+ heroTitle = postsPage?.title || "Blog";
58
+ heroBg = postsPage?.featuredImage?.sourceUrl ?? null;
59
+ heroOverlay = postsPage?.overlayOpacity ?? 0.6;
60
+ }
61
+
41
62
  const fmtDate = (d: string) =>
42
63
  new Date(d).toLocaleDateString("en-US", { year: "numeric", month: "long", day: "2-digit" });
43
64
 
@@ -23,7 +23,22 @@ export async function getStaticPaths() {
23
23
  }));
24
24
  }
25
25
 
26
- const { post, posts, previous, next } = Astro.props;
26
+ let { post, posts, previous, next } = Astro.props;
27
+
28
+ // Dev only: re-fetch on each request so WP edits show on refresh without
29
+ // restarting `astro dev` (getStaticPaths props are cached otherwise). Stripped
30
+ // from production builds (import.meta.env.DEV).
31
+ if (import.meta.env.DEV) {
32
+ const all = await getPosts();
33
+ const i = all.findIndex((p) => p.slug === Astro.params.slug);
34
+ if (i !== -1) {
35
+ posts = all;
36
+ post = all[i];
37
+ previous = all[i + 1] ?? null;
38
+ next = all[i - 1] ?? null;
39
+ }
40
+ }
41
+
27
42
  const img = post.featuredImage;
28
43
  const fmtDate = (d: string) =>
29
44
  new Date(d).toLocaleDateString("en-US", { year: "numeric", month: "long", day: "2-digit" });
@@ -28,7 +28,20 @@ export async function getStaticPaths() {
28
28
  }));
29
29
  }
30
30
 
31
- const { doc, tree } = Astro.props;
31
+ let { doc, tree } = Astro.props;
32
+
33
+ // Dev only: re-fetch on each request so WP doc edits show on refresh without
34
+ // restarting `astro dev` (getStaticPaths props are cached otherwise). Stripped
35
+ // from production builds (import.meta.env.DEV).
36
+ if (import.meta.env.DEV) {
37
+ const toSlug = (uri: string) => uri.replace(/^\/+|\/+$/g, "").replace(/^docs\/?/, "");
38
+ const docs = await getDocs();
39
+ const fresh = docs.find((d) => toSlug(d.uri) === Astro.params.slug);
40
+ if (fresh) {
41
+ doc = fresh;
42
+ tree = buildDocTree(docs);
43
+ }
44
+ }
32
45
  ---
33
46
  <Base pageTitle={doc.title} description={doc.excerpt}>
34
47
  <HeroHeader title="Documentation" subhead="" backgroundImage={null} overlayOpacity={0.6} />