Skip to content

Commit 306cadd

Browse files
chore(gatsby): migrate normalize-page-path to typescript (#22188)
1 parent 4404af1 commit 306cadd

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

packages/gatsby/src/utils/normalize-page-path.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// This is a duplicate of the runtime util
2+
// file cache-dir/normalize-page-path.js
3+
// While still a duplicate, this one is
4+
// rewritten in typescript
5+
export function normalizePagePath(path: string): string {
6+
if (path === undefined) {
7+
return path
8+
}
9+
if (path === `/`) {
10+
return `/`
11+
}
12+
if (path.charAt(path.length - 1) === `/`) {
13+
return path.slice(0, -1)
14+
}
15+
return path
16+
}
17+
18+
export function denormalizePagePath(path: string): string {
19+
if (path === undefined) {
20+
return path
21+
}
22+
if (path === `/`) {
23+
return `/`
24+
}
25+
if (path.charAt(path.length - 1) !== `/`) {
26+
return path + `/`
27+
}
28+
return path
29+
}

packages/gatsby/src/utils/websocket-manager.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require(`path`)
44
const { store } = require(`../redux`)
55
const fs = require(`fs`)
66
const pageDataUtil = require(`../utils/page-data`)
7-
const normalizePagePath = require(`../utils/normalize-page-path`)
7+
import { normalizePagePath, denormalizePagePath } from "./normalize-page-path"
88
const telemetry = require(`gatsby-telemetry`)
99
const url = require(`url`)
1010
const { createHash } = require(`crypto`)
@@ -16,19 +16,6 @@ type QueryResult = {
1616

1717
type QueryResultsMap = Map<string, QueryResult>
1818

19-
const denormalize = path => {
20-
if (path === undefined) {
21-
return path
22-
}
23-
if (path === `/`) {
24-
return `/`
25-
}
26-
if (path.charAt(path.length - 1) !== `/`) {
27-
return path + `/`
28-
}
29-
return path
30-
}
31-
3219
/**
3320
* Get cached page query result for given page path.
3421
* @param {string} pagePath Path to a page.
@@ -40,7 +27,7 @@ const getCachedPageData = async (
4027
): QueryResult => {
4128
const { program, pages } = store.getState()
4229
const publicDir = path.join(program.directory, `public`)
43-
if (pages.has(denormalize(pagePath)) || pages.has(pagePath)) {
30+
if (pages.has(denormalizePagePath(pagePath)) || pages.has(pagePath)) {
4431
try {
4532
const pageData = await pageDataUtil.read({ publicDir }, pagePath)
4633

0 commit comments

Comments
 (0)