@justanarthur/payload-www 1.4.1 → 2.0.0

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.
@@ -1,7 +1,4 @@
1
-
2
-
3
1
  // src/render/pages/FooterPage.tsx
4
- import"server-only";
5
2
  import { jsx, jsxs } from "react/jsx-runtime";
6
3
  function FooterPage({ data }) {
7
4
  const nav = data.nav ?? [];
@@ -59,7 +56,6 @@ function FooterPage({ data }) {
59
56
  }
60
57
 
61
58
  // src/render/pages/HeaderPage.tsx
62
- import"server-only";
63
59
  import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
64
60
  function HeaderPage({ data }) {
65
61
  const nav = data.nav ?? [];
@@ -207,9 +203,6 @@ async function PostsPage({ doc, locale, ...props }) {
207
203
  });
208
204
  }
209
205
 
210
- // src/render/pages/createCollectionPageExports.tsx
211
- import"server-only";
212
-
213
206
  // src/render/metadata/slug.ts
214
207
  var SLUG_NESTED_DIVIDER = "_";
215
208
  function paramsSlugToSlug(slug, _shape) {
@@ -229,142 +222,118 @@ function slugToPath(slug) {
229
222
  }
230
223
 
231
224
  // src/render/metadata/query.ts
225
+ import { createCacheHelpers } from "@pro-laico/payload-revalidate/cache";
226
+ import { tagsFor } from "@pro-laico/payload-revalidate";
227
+ import { cacheLife } from "next/cache";
232
228
  import { getPayload } from "payload";
233
-
234
- // src/collections/hooks/createRevalidateCollectionGlobalHook.ts
235
- import { revalidateTag } from "next/cache";
236
- function createRevalidateCollectionGlobalHook() {
237
- const afterChangeDeleteHook = async ({ doc, req, ...args }) => {
238
- const previousDoc = "previousDoc" in args ? args.previousDoc : null, locale = req.locale;
239
- function revalidate(args2) {
240
- try {
241
- revalidateTag(createCollectionCacheKey({ ...args2, locale }), "max");
242
- } catch (error) {
243
- const msg = error instanceof Error ? error.message : String(error);
244
- if (msg.includes("static generation store missing"))
245
- return;
246
- console.error(error);
247
- }
248
- }
249
- if ("collection" in args) {
250
- [
251
- doc && doc._status !== "draft" && doc.slug,
252
- previousDoc && previousDoc._status === "published" && previousDoc.slug
253
- ].filter((slug) => slug !== undefined && slug !== false).map((slug) => revalidate({ collectionSlug: args.collection.slug, slug }));
254
- } else {
255
- revalidate({ globalSlug: args.global.slug });
256
- }
257
- return doc;
258
- };
259
- return { afterChange: afterChangeDeleteHook, afterDelete: afterChangeDeleteHook };
260
- }
261
- function createCollectionCacheKey(args) {
262
- return `${"globalSlug" in args ? args.globalSlug : args.collectionSlug + args.slug}_${args.locale}`;
229
+ var _helpers = null;
230
+ var _seedPromise = null;
231
+ function seedPayloadCache({ config }) {
232
+ if (_helpers || _seedPromise)
233
+ return;
234
+ _seedPromise = (async () => {
235
+ const payload = await getPayload({ config: await config });
236
+ _helpers = createCacheHelpers(payload);
237
+ })();
263
238
  }
264
-
265
- // src/render/metadata/query.ts
266
- async function withUnstableCache(keyParts, tags, fn) {
267
- const { unstable_cache } = await import("next/cache");
268
- return unstable_cache(fn, keyParts.map(String), { tags })();
239
+ async function requireCacheHelpers() {
240
+ if (_helpers)
241
+ return _helpers;
242
+ if (_seedPromise) {
243
+ await _seedPromise;
244
+ if (_helpers)
245
+ return _helpers;
246
+ }
247
+ throw new Error("[payload-www] seedPayloadCache({ config }) must be called before any query getter. " + "Call it from createCollectionPageExports / createRootLayoutExports factory, or in your root layout.");
269
248
  }
270
- async function queryDoc(args, { config: configPromise }) {
271
- return withUnstableCache(Object.values(args), [createCollectionCacheKey(args)], async () => {
272
- if ("globalSlug" in args)
273
- return queryGlobal({ ...args, config: configPromise });
274
- else
275
- return queryDocBySlug({ ...args, config: configPromise });
249
+ async function queryDocBySlug(args) {
250
+ "use cache";
251
+ cacheLife("weeks");
252
+ const { findDoc } = await requireCacheHelpers();
253
+ const slugField = args.slugField ?? "slug";
254
+ const result = await findDoc(args.collectionSlug, {
255
+ where: { [slugField]: { equals: args.slug } },
256
+ locale: args.locale,
257
+ draft: args.draft ?? false,
258
+ depth: args.depth
276
259
  });
260
+ return result ?? null;
277
261
  }
278
- async function queryDocBySlug({
279
- collectionSlug,
280
- slug,
281
- slugField = "slug",
282
- locale,
283
- draft = false,
284
- config
285
- }) {
286
- return withUnstableCache([collectionSlug, slug, locale, draft], [createCollectionCacheKey({ collectionSlug, slug, locale })], async () => {
287
- const payload = await getPayload({ config });
288
- const result = await payload.find({
289
- collection: collectionSlug,
290
- draft,
291
- limit: 1,
292
- pagination: false,
293
- overrideAccess: draft,
294
- where: { [slugField]: { equals: slug } },
295
- locale
262
+ async function queryGlobal(args) {
263
+ "use cache";
264
+ cacheLife("weeks");
265
+ const { findGlobal } = await requireCacheHelpers();
266
+ try {
267
+ const result = await findGlobal(args.globalSlug, {
268
+ locale: args.locale,
269
+ draft: args.draft ?? false,
270
+ depth: args.depth
296
271
  });
297
- return result.docs?.[0] ?? null;
298
- });
272
+ return result ?? null;
273
+ } catch (error) {
274
+ console.warn("[WWW] queryGlobal failed", { globalSlug: args.globalSlug, locale: args.locale, error: String(error) });
275
+ return null;
276
+ }
299
277
  }
300
- async function queryGlobal({
301
- globalSlug,
302
- locale,
303
- draft = false,
304
- config
305
- }) {
306
- return withUnstableCache([globalSlug, locale, draft], [createCollectionCacheKey({ globalSlug, locale })], async () => {
307
- const payload = await getPayload({ config });
308
- try {
309
- return await payload.findGlobal({ slug: globalSlug, draft, locale });
310
- } catch (error) {
311
- console.warn("[WWW] queryGlobal failed", { globalSlug, locale, error: String(error) });
312
- return null;
313
- }
314
- });
278
+ async function queryAllDocs(args) {
279
+ "use cache";
280
+ cacheLife("weeks");
281
+ const { findIds, findDocByID } = await requireCacheHelpers();
282
+ const collection = args.collectionSlug;
283
+ const { ids } = await findIds(collection, { locale: args.locale });
284
+ if (ids.length === 0)
285
+ return [];
286
+ const docs = await Promise.all(ids.map((id) => findDocByID(collection, id, { locale: args.locale })));
287
+ return docs.filter((d) => d !== null);
315
288
  }
316
- async function queryAllDocs({
317
- collectionSlug,
318
- slugField = "slug",
319
- locale,
320
- config
321
- }) {
322
- return withUnstableCache([collectionSlug, slugField, locale], [createCollectionCacheKey({ collectionSlug, slug: "__all__", locale })], async () => {
323
- const payload = await getPayload({ config });
324
- const result = await payload.find({
325
- collection: collectionSlug,
326
- draft: false,
327
- limit: 1000,
328
- pagination: false,
329
- overrideAccess: false,
330
- select: { [slugField]: true },
331
- locale
332
- });
333
- return result.docs ?? [];
334
- });
289
+ async function queryDoc(args) {
290
+ if ("globalSlug" in args)
291
+ return queryGlobal(args);
292
+ return queryDocBySlug(args);
335
293
  }
336
- async function queryAllLocaleSlugs({
337
- collectionSlug,
338
- id,
339
- slugField = "slug",
340
- config
341
- }) {
342
- const payload = await getPayload({ config });
343
- const doc = await payload.findByID({
344
- collection: collectionSlug,
345
- id,
346
- locale: "all",
347
- select: { [slugField]: true }
294
+ async function queryAllLocaleSlugs(args) {
295
+ "use cache";
296
+ cacheLife("weeks");
297
+ const { findDocByID } = await requireCacheHelpers();
298
+ const slugField = args.slugField ?? "slug";
299
+ const doc = await findDocByID(args.collectionSlug, args.id, { locale: "all", select: { [slugField]: true } });
300
+ const localeMap = doc?.[slugField];
301
+ if (localeMap && typeof localeMap === "object")
302
+ return localeMap;
303
+ return null;
304
+ }
305
+ async function queryDocByID(args) {
306
+ "use cache";
307
+ cacheLife("weeks");
308
+ const { findDocByID } = await requireCacheHelpers();
309
+ return await findDocByID(args.collectionSlug, args.id, {
310
+ locale: args.locale,
311
+ draft: args.draft ?? false,
312
+ depth: args.depth
348
313
  });
349
- return doc?.[slugField];
350
314
  }
351
315
 
352
316
  // src/render/pages/createCollectionPageExports.tsx
353
317
  import { setRequestLocale } from "next-intl/server";
354
318
 
355
319
  // src/render/pages/utils/buildLocalizedPath.ts
320
+ function resolvePagePathPrefix(prefix, locale, routing) {
321
+ if (typeof prefix !== "object")
322
+ return prefix;
323
+ return prefix[locale] ?? prefix[routing.defaultLocale];
324
+ }
356
325
  function buildLocalizedPath(locale, prefix, slug, { routing }) {
357
326
  const path = slugToPath(slug);
327
+ const localePrefix = resolvePagePathPrefix(prefix, locale, routing);
358
328
  return `${routing.localePrefix === "never" ? "" : routing.localePrefix === "always" ? locale : routing.localePrefix === "as-needed" ? locale === routing.defaultLocale ? "" : "/" + locale : (() => {
359
329
  throw new Error("Unsupported locale prefix");
360
- })()}${prefix ? "/" + prefix : ""}${path ? "/" + path : ""}`;
330
+ })()}${localePrefix ? "/" + localePrefix : ""}${path ? "/" + path : ""}`;
361
331
  }
362
332
  function buildLocalizedPaths(localesSlug, pagePathPrefix, { routing }) {
363
- if (!localesSlug[routing.defaultLocale])
364
- return {};
333
+ const blankIsHome = !localesSlug[routing.defaultLocale];
365
334
  return routing.locales.reduce((paths, locale) => {
366
335
  const slug = localesSlug[locale];
367
- if (!slug)
336
+ if (!slug && !blankIsHome)
368
337
  return paths;
369
338
  paths[locale] = buildLocalizedPath(locale, pagePathPrefix, slug, { routing });
370
339
  return paths;
@@ -413,7 +382,7 @@ async function renderWWWDataModule(data, {
413
382
  import { jsx as jsx7, Fragment as Fragment2 } from "react/jsx-runtime";
414
383
  function createCollectionPageExports({
415
384
  slug: collectionSlug = "pages",
416
- config: configPromise,
385
+ _payloadConfig,
417
386
  importMap,
418
387
  routing,
419
388
  slugShape = "single"
@@ -421,14 +390,13 @@ function createCollectionPageExports({
421
390
  getServerSideURL,
422
391
  pagePathPrefix
423
392
  }) {
393
+ seedPayloadCache({ config: _payloadConfig });
424
394
  const siteUrl = getServerSideURL();
425
395
  async function fetchDoc(locale, slug) {
426
396
  return queryDoc({
427
397
  slug,
428
398
  locale,
429
399
  collectionSlug
430
- }, {
431
- config: configPromise
432
400
  });
433
401
  }
434
402
  const default_ = async (props) => {
@@ -445,22 +413,25 @@ function createCollectionPageExports({
445
413
  const { notFound } = await import("next/navigation");
446
414
  notFound();
447
415
  }
448
- const rendered = renderWWWDataModule(doc, { collectionSlug, config: configPromise, importMap }, { ...props, locale });
416
+ const rendered = renderWWWDataModule(doc, { collectionSlug, config: _payloadConfig, importMap }, { ...props, locale });
449
417
  return /* @__PURE__ */ jsx7(Fragment2, {
450
418
  children: rendered
451
419
  });
452
420
  };
453
421
  async function generateMetadata(props) {
454
422
  const params = await props.params;
455
- const locale = params.locale, slug = paramsSlugToSlug(params.slug, slugShape);
423
+ const locale = params.locale;
424
+ if (!routing.locales.includes(locale)) {
425
+ return {};
426
+ }
427
+ const slug = paramsSlugToSlug(params.slug, slugShape);
456
428
  const doc = await fetchDoc(locale, slug);
457
429
  const [localesSlug, siteDefaults] = await Promise.all([
458
430
  doc ? queryAllLocaleSlugs({
459
431
  id: doc.id,
460
- collectionSlug,
461
- config: configPromise
432
+ collectionSlug
462
433
  }) : Promise.resolve({}),
463
- createSiteDefaults({ config: configPromise, locale })
434
+ createSiteDefaults({ config: _payloadConfig, locale })
464
435
  ]);
465
436
  if (!doc)
466
437
  return {};
@@ -477,22 +448,23 @@ function createCollectionPageExports({
477
448
  return { ...meta, alternates };
478
449
  }
479
450
  async function generateStaticParams(props) {
480
- const locale = (await props.params).locale;
481
- const docs = await queryAllDocs({ locale, collectionSlug, config: configPromise });
482
- return docs.filter((doc) => typeof doc.slug === "string" && doc.slug.length > 0).map((doc) => ({ slug: slugToParamsSlug(doc.slug, slugShape) }));
451
+ await props.params;
452
+ const perLocaleEntries = await Promise.all(routing.locales.map(async (locale) => {
453
+ const docs = await queryAllDocs({ locale, collectionSlug });
454
+ return docs.filter((doc) => typeof doc.slug === "string" && doc.slug.length > 0).map((doc) => ({ locale, slug: slugToParamsSlug(doc.slug, slugShape) }));
455
+ }));
456
+ return perLocaleEntries.flat();
483
457
  }
484
458
  async function generateSitemap() {
485
459
  const locale = routing.defaultLocale;
486
460
  const docs = await queryAllDocs({
487
461
  collectionSlug,
488
- locale,
489
- config: configPromise
462
+ locale: routing.defaultLocale
490
463
  });
491
464
  return await Promise.all(docs.map(async (doc) => {
492
465
  const localesSlug = await queryAllLocaleSlugs({
493
466
  id: doc.id,
494
- collectionSlug,
495
- config: configPromise
467
+ collectionSlug
496
468
  }) ?? {};
497
469
  const alternates = buildAlternates(locale, localesSlug, pagePathPrefix, { routing, siteUrl });
498
470
  return {
@@ -503,7 +475,7 @@ function createCollectionPageExports({
503
475
  }));
504
476
  }
505
477
  generateSitemap.getServerSideURL = getServerSideURL;
506
- generateSitemap.pagePathPrefix = pagePathPrefix;
478
+ generateSitemap.pagePathPrefix = resolvePagePathPrefix(pagePathPrefix, routing.defaultLocale, routing);
507
479
  return {
508
480
  default: default_,
509
481
  generateMetadata,
@@ -527,13 +499,13 @@ ${sitemaps.map((url) => ` <sitemap><loc>${url}</loc></sitemap>`).join(`
527
499
  }
528
500
 
529
501
  // src/render/pages/createRootLayoutExports.tsx
530
- import"server-only";
502
+ import { Activity } from "react";
531
503
  import { setRequestLocale as setRequestLocale2 } from "next-intl/server";
532
504
  import { NextIntlClientProvider } from "next-intl";
533
505
  import { RootJsonLd } from "@justanarthur/payload-plugin-seo/root-jsonld";
534
506
  import { jsx as jsx8, jsxs as jsxs4 } from "react/jsx-runtime";
535
507
  function createRootLayoutExports({
536
- config: configPromise,
508
+ _payloadConfig,
537
509
  importMap,
538
510
  routing
539
511
  }, {
@@ -541,6 +513,7 @@ function createRootLayoutExports({
541
513
  htmlAttrs,
542
514
  getServerSideURL
543
515
  } = {}) {
516
+ seedPayloadCache({ config: _payloadConfig });
544
517
  async function RootLayout(props) {
545
518
  const params = await props.params;
546
519
  const locale = params.locale;
@@ -553,17 +526,17 @@ function createRootLayoutExports({
553
526
  header,
554
527
  footer
555
528
  ] = await Promise.all([
556
- queryDoc({ globalSlug: "header", locale }, { config: configPromise }),
557
- queryDoc({ globalSlug: "footer", locale }, { config: configPromise })
529
+ queryDoc({ globalSlug: "header", locale }),
530
+ queryDoc({ globalSlug: "footer", locale })
558
531
  ]);
559
- const renderedHeader = renderWWWDataModule(header, { collectionSlug: "header", configPath: "globals", config: configPromise, importMap }, { ...props, locale }), renderedFooter = renderWWWDataModule(footer, { collectionSlug: "footer", configPath: "globals", config: configPromise, importMap }, { ...props, locale });
532
+ const renderedHeader = renderWWWDataModule(header, { collectionSlug: "header", configPath: "globals", config: _payloadConfig, importMap }, { ...props, locale }), renderedFooter = renderWWWDataModule(footer, { collectionSlug: "footer", configPath: "globals", config: _payloadConfig, importMap }, { ...props, locale });
560
533
  const mergedHtmlAttrs = {
561
534
  lang: locale,
562
535
  suppressHydrationWarning: true,
563
536
  ...htmlAttrs?.(locale) ?? {}
564
537
  };
565
538
  const rootJsonLd = getServerSideURL ? /* @__PURE__ */ jsx8(RootJsonLd, {
566
- config: configPromise,
539
+ config: _payloadConfig,
567
540
  locale,
568
541
  getServerSideURL,
569
542
  locales: routing.locales
@@ -574,9 +547,13 @@ function createRootLayoutExports({
574
547
  children: /* @__PURE__ */ jsxs4(NextIntlClientProvider, {
575
548
  children: [
576
549
  rootJsonLd,
577
- renderedHeader,
550
+ /* @__PURE__ */ jsx8(Activity, {
551
+ children: renderedHeader
552
+ }),
578
553
  providers ? providers({ children: props.children, locale }) : props.children,
579
- renderedFooter
554
+ /* @__PURE__ */ jsx8(Activity, {
555
+ children: renderedFooter
556
+ })
580
557
  ]
581
558
  })
582
559
  })
@@ -593,12 +570,12 @@ import { RootJsonLd as RootJsonLd2 } from "@justanarthur/payload-plugin-seo/root
593
570
  var renderPages = { createCollectionPageExports, createRootLayoutExports, PagesPage, PostsPage, HeaderPage, FooterPage, RootJsonLd: RootJsonLd2 };
594
571
  var render_pages_default = renderPages;
595
572
  export {
596
- render_pages_default as default,
597
- createRootLayoutExports,
598
- createCollectionPageExports,
599
- RootJsonLd2 as RootJsonLd,
600
- PostsPage,
601
- PagesPage,
573
+ FooterPage,
602
574
  HeaderPage,
603
- FooterPage
575
+ PagesPage,
576
+ PostsPage,
577
+ RootJsonLd2 as RootJsonLd,
578
+ createCollectionPageExports,
579
+ createRootLayoutExports,
580
+ render_pages_default as default
604
581
  };
package/dist/sitemap.d.ts CHANGED
@@ -1,10 +1,23 @@
1
+ /**
2
+ * URL segment a collection is mounted under. A string is used for every locale; a record
3
+ * localizes it (`{ en: 'posts', sk: 'prispevky' }`), so a host serving localized routes
4
+ * gets canonical/hreflang URLs that match the URLs it actually serves. Locales missing
5
+ * from the record fall back to the default locale.
6
+ */
7
+ type PagePathPrefix = string | Record<string, string>;
1
8
  type CreateCollectionPageExportsDeps<S extends string> = {
2
9
  getServerSideURL: () => string;
3
- pagePathPrefix?: string;
10
+ pagePathPrefix?: PagePathPrefix;
4
11
  };
5
12
  type GenerateSitemapPageExportsArgs = CreateCollectionPageExportsDeps<any>;
6
13
  declare function createSitemapFromCollections(...args: GenerateSitemapPageExportsArgs[]): () => Response;
14
+ import { SanitizedConfig as SanitizedConfig2 } from "payload";
15
+ type SeedPayloadCacheArgs = {
16
+ config: SanitizedConfig2 | Promise<SanitizedConfig2>;
17
+ };
18
+ declare function seedPayloadCache({ config }: SeedPayloadCacheArgs): void;
7
19
  declare const _default: {
8
20
  createSitemapFromCollections: typeof createSitemapFromCollections;
21
+ seedPayloadCache: typeof seedPayloadCache;
9
22
  };
10
- export { _default as default, createSitemapFromCollections };
23
+ export { createSitemapFromCollections, _default as default, seedPayloadCache };