Skip to content

Commit

Permalink
chore(gatsby): migrate normalize-page-path to typescript (#22188)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnjanssen committed Mar 11, 2020
1 parent 4404af1 commit 306cadd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
14 changes: 0 additions & 14 deletions packages/gatsby/src/utils/normalize-page-path.js

This file was deleted.

29 changes: 29 additions & 0 deletions packages/gatsby/src/utils/normalize-page-path.ts
@@ -0,0 +1,29 @@
// This is a duplicate of the runtime util
// file cache-dir/normalize-page-path.js
// While still a duplicate, this one is
// rewritten in typescript
export function normalizePagePath(path: string): string {
if (path === undefined) {
return path
}
if (path === `/`) {
return `/`
}
if (path.charAt(path.length - 1) === `/`) {
return path.slice(0, -1)
}
return path
}

export function denormalizePagePath(path: string): string {
if (path === undefined) {
return path
}
if (path === `/`) {
return `/`
}
if (path.charAt(path.length - 1) !== `/`) {
return path + `/`
}
return path
}
17 changes: 2 additions & 15 deletions packages/gatsby/src/utils/websocket-manager.js
Expand Up @@ -4,7 +4,7 @@ const path = require(`path`)
const { store } = require(`../redux`)
const fs = require(`fs`)
const pageDataUtil = require(`../utils/page-data`)
const normalizePagePath = require(`../utils/normalize-page-path`)
import { normalizePagePath, denormalizePagePath } from "./normalize-page-path"
const telemetry = require(`gatsby-telemetry`)
const url = require(`url`)
const { createHash } = require(`crypto`)
Expand All @@ -16,19 +16,6 @@ type QueryResult = {

type QueryResultsMap = Map<string, QueryResult>

const denormalize = path => {
if (path === undefined) {
return path
}
if (path === `/`) {
return `/`
}
if (path.charAt(path.length - 1) !== `/`) {
return path + `/`
}
return path
}

/**
* Get cached page query result for given page path.
* @param {string} pagePath Path to a page.
Expand All @@ -40,7 +27,7 @@ const getCachedPageData = async (
): QueryResult => {
const { program, pages } = store.getState()
const publicDir = path.join(program.directory, `public`)
if (pages.has(denormalize(pagePath)) || pages.has(pagePath)) {
if (pages.has(denormalizePagePath(pagePath)) || pages.has(pagePath)) {
try {
const pageData = await pageDataUtil.read({ publicDir }, pagePath)

Expand Down

0 comments on commit 306cadd

Please sign in to comment.