Skip to content

Commit

Permalink
fix: ignore fragment-only links (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlexLichter authored Dec 15, 2024
1 parent 65c1cb7 commit 64c44db
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/runtime/shared/inspections/trailing-slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export default function RuleTrailingSlash() {
id: 'trailing-slash',
test({ report, link, siteConfig }) {
const $url = parseURL(link)

// Ignore fragment-only links
if ($url.pathname === '' && $url.hash) {
return
}

// its a file when the last segment has a dot in it
const isFile = $url.pathname.split('/').pop()!.includes('.')
if ($url.pathname === '/' || isFile)
Expand Down
51 changes: 51 additions & 0 deletions test/unit/rules/trailing-slash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,55 @@ describe('rule trailing-slash', () => {
}
`)
})

it('works with fragment-only links', () => {
const ctx = {
siteConfig: {
trailingSlash: true,
},
response: { status: 200, statusText: 'OK', headers: {} },
link: '#abc',
} as RuleTestContext

expect(runRule(ctx, RuleTrailingSlash())).toMatchInlineSnapshot(`
{
"error": [],
"fix": "#abc",
"link": "#abc",
"passes": true,
"textContent": undefined,
"warning": [],
}
`)
})

it('works with links containing fragments', () => {
const ctx = {
siteConfig: {
trailingSlash: true,
},
response: { status: 200, statusText: 'OK', headers: {} },
link: '/a#abc',
} as RuleTestContext

expect(runRule(ctx, RuleTrailingSlash())).toMatchInlineSnapshot(`
{
"error": [],
"fix": "/a/#abc",
"link": "/a#abc",
"passes": false,
"textContent": undefined,
"warning": [
{
"fix": "/a/#abc",
"fixDescription": "Add trailing slash.",
"message": "Should have a trailing slash.",
"name": "trailing-slash",
"scope": "warning",
"tip": "Incorrect trailing slashes can cause duplicate pages in search engines and waste crawl budget.",
},
],
}
`)
})
})

0 comments on commit 64c44db

Please sign in to comment.