Skip to content

[gatsby-plugin-preload-fonts] handle trailing slashes in onRenderBody #21814

Description

@nibtime

Description

I used the plugin and observed, that in some HTML files the preload links for the fonts were missing.
Then I saw, that in font-preload-cache.json, some routes contain a trailing slash, but I generate all my pages without a trailing slash. For those routes, the lookup in the asset map fails.

if (!cache.assets[pathname]) return

This could be replaced with

const assetsOfPath = cache.assets[pathname] ? cache.assets[pathname] : cache.assets[pathname + "/"]
if (!assetsOfPath) {
  return
}
const assets = Object.keys(assetsOfPath)

Steps to reproduce

Generate some pages at paths with no trailing slashes. I am not sure why sometimes paths are written with trailing slashes and sometimes not in font-preload-cache.json. However, if that happens, lookups will fail in onRenderBody.

Expected result

Preload links should be written in all HTML files with a path match , independent of trailing slashes

Actual result

Preload links are not written in HTML files with a non-trailing-slash path, if asset map contains the same path with a trailing slash as a key for the assets

Metadata

Metadata

Assignees

Labels

type: bugAn issue or pull request relating to a bug in Gatsby

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions