mirror of
https://github.com/svemagie/blog-eleventy-indiekit.git
synced 2026-04-02 16:44:56 +02:00
Eleventy 3.x renders Nunjucks templates in parallel, causing page.url
to return wrong values in {% set %} tags. This caused OG images to be
mismatched between pages (e.g., bookmark showed note's OG image).
Move ogSlug and hasOgImage computation to eleventyComputed, which runs
during the sequential data cascade phase before parallel rendering.
The computed values are then available as plain template variables.
Refs: https://github.com/11ty/eleventy/issues/3183
34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
/**
|
|
* Computed data resolved during the data cascade (sequential, per-page).
|
|
*
|
|
* Eleventy 3.x renders Nunjucks templates in parallel, which causes
|
|
* `page.url` and `page.fileSlug` to return wrong values when read
|
|
* via {% set %} in templates. By computing OG-related values here,
|
|
* they are resolved before parallel rendering begins.
|
|
*
|
|
* See: https://github.com/11ty/eleventy/issues/3183
|
|
*/
|
|
|
|
import { existsSync } from "node:fs";
|
|
import { resolve, dirname } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
export default {
|
|
ogSlug: (data) => {
|
|
const url = data.page?.url;
|
|
if (!url) return "";
|
|
return url.replace(/\/$/, "").split("/").pop();
|
|
},
|
|
|
|
hasOgImage: (data) => {
|
|
const url = data.page?.url;
|
|
if (!url) return false;
|
|
const slug = url.replace(/\/$/, "").split("/").pop();
|
|
if (!slug) return false;
|
|
const ogPath = resolve(__dirname, "..", ".cache", "og", `${slug}.png`);
|
|
return existsSync(ogPath);
|
|
},
|
|
};
|