Skip to content

Inject pageUrl into page vars automatically#250

Open
bcomnes wants to merge 3 commits intomasterfrom
fix/page-url-var
Open

Inject pageUrl into page vars automatically#250
bcomnes wants to merge 3 commits intomasterfrom
fix/page-url-var

Conversation

@bcomnes
Copy link
Copy Markdown
Owner

@bcomnes bcomnes commented Apr 18, 2026

Every page now receives a pageUrl variable derived from its output location, so layouts can construct canonical URLs without manually computing the path convention.

// Before: layout had to do this manually
const pageUrl = page.path ? '/' + page.path + '/' : '/'

// After: pageUrl is available in vars directly
const canonicalUrl = `${vars.siteUrl}${vars.pageUrl}`

The URL is computed based on the page's outputName:

  • Index pages (outputName === 'index.html'): get a trailing-slash URL, e.g. /blog/my-post/, or / for the root.
  • Non-index pages (loose markdown like loose-md.html): get a URL that includes the filename, e.g. /md-page/loose-md.html.

This means pageUrl does not always end with a trailing slash — layouts that need to link to loose markdown pages will get the full path including the filename.

The value is injected at the base of the vars merge, so user-supplied pageUrl in page.vars.js or global.vars.js still takes precedence. fsPathToUrlPath is used to normalize path separators for cross-platform safety.

Closes #238

Every page now gets a pageUrl derived from pageInfo.path so layouts can
build canonical URLs without manually computing the path-to-URL conversion.
User-supplied pageUrl in pageVars or globalVars still takes precedence.

Closes #238
Copilot AI review requested due to automatic review settings April 18, 2026 18:43
@coveralls
Copy link
Copy Markdown

coveralls commented Apr 18, 2026

Coverage Report for CI Build 24619422519

Coverage increased (+0.07%) to 91.537%

Details

  • Coverage increased (+0.07%) from the base build.
  • Patch coverage: 28 of 28 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4098
Covered Lines: 3832
Line Coverage: 93.51%
Relevant Branches: 652
Covered Branches: 516
Branch Coverage: 79.14%
Branches in Coverage %: Yes
Coverage Strength: 77.68 hits per line

💛 - Coveralls

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make canonical URL construction easier in layouts by automatically injecting a pageUrl value into PageData.vars, derived from pageInfo.

Changes:

  • Inject pageUrl into the base of the vars merge inside PageData.vars, allowing user-defined pageUrl to override it via later spreads.
Comments suppressed due to low confidence (1)

lib/build-pages/page-data.js:149

  • New public-ish behavior (vars.pageUrl) isn’t covered by tests. The repo already has node:test coverage for build behavior (eg test-cases/general-features/index.test.js). Please add a regression test that asserts vars.pageUrl (or an HTML marker rendered from it) for both an index page (/some-dir/) and a loose markdown page (/some-dir/file.html), and ideally cover Windows path normalization as well.
      pageUrl: this.pageInfo.path ? `/${this.pageInfo.path}/` : '/',
      ...globalVars,
      ...globalDataVars,
      ...pageVars,
      ...builderVars,
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/build-pages/page-data.js Outdated
Comment thread lib/build-pages/page-data.js Outdated
Loose markdown files (not page.md/README.md) have outputName set to
their slug.html rather than index.html, so the previous formula
produced an incorrect directory-style URL for them.

The URL is now derived from outputName: index pages get a trailing slash,
non-index pages include the filename. fsPathToUrlPath is used to
normalize any OS-specific path separators.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes vars.pageUrl available to every rendered page by computing it from pageInfo inside PageData.vars, allowing layouts to build canonical/OG URLs without duplicating the project’s URL-construction convention.

Changes:

  • Injects a computed pageUrl into PageData.vars at the base of the vars merge (so user overrides still win).
  • Computes pageUrl with special handling for index.html (trailing-slash URLs) vs non-index outputs (include filename).
  • Normalizes URL paths via fsPathToUrlPath() for cross-platform (Windows) safety.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/build-pages/page-data.js Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds automatic vars.pageUrl injection to every page so layouts can build canonical URLs without manually reconstructing URL path conventions.

Changes:

  • Inject pageUrl into PageData.vars at the lowest precedence so user-defined vars can override it.
  • Compute pageUrl based on pageInfo.path + outputName, mapping index.html to trailing-slash URLs and including filenames for non-index outputs.
  • Normalize path separators via fsPathToUrlPath() for cross-platform URL consistency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/build-pages/page-data.js Outdated
…ules

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pageUrl not available to layouts for directly-rendered pages

3 participants