Skip to content

Commit

Permalink
upgrade performance plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Aug 11, 2021
1 parent 61f3523 commit b18196a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
32 changes: 19 additions & 13 deletions packages/gatsby-plugin-performance/cache-dir/find-path.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// This file is copied from gatsby's cache-dir folder. The only part we change is the code related to path matching with matchPaths variable. We leave all unecessary code commented up to make it easier for future PRs to support newer versions of gatsby
// import { pick } from "@gatsbyjs/reach-router/lib/utils"
import stripPrefix from "./strip-prefix"
import normalizePagePath from "./normalize-page-path"
import { pick } from '@gatsbyjs/reach-router/lib/utils'
import stripPrefix from './strip-prefix'
import normalizePagePath from './normalize-page-path'
import { maybeGetBrowserRedirect } from './redirect-utils.js'

const pathCache = new Map()
// let matchPaths = []

const trimPathname = rawPathname => {
const trimPathname = (rawPathname) => {
const pathname = decodeURIComponent(rawPathname)
// Remove the pathPrefix from the pathname.
const trimmedPathname = stripPrefix(pathname, __BASE_PATH__)
const trimmedPathname = stripPrefix(
pathname,
decodeURIComponent(__BASE_PATH__)
)
// Remove any hashfragment
.split(`#`)[0]
// Remove search query
Expand Down Expand Up @@ -40,7 +43,7 @@ function absolutify(path) {
*
* @param {Array<{path: string, matchPath: string}>} value collection of matchPaths
*/
export const setMatchPaths = value => {
export const setMatchPaths = (value) => {
// matchPaths = value
}

Expand All @@ -52,7 +55,7 @@ export const setMatchPaths = value => {
* @param {string} rawPathname A raw pathname
* @return {string|null}
*/
export const findMatchPath = rawPathname => {
export const findMatchPath = (rawPathname) => {
const trimmedPathname = cleanPath(rawPathname)

// const pickPaths = matchPaths.map(({ path, matchPath }) => {
Expand All @@ -68,8 +71,6 @@ export const findMatchPath = rawPathname => {
// return normalizePagePath(path.route.originalPath)
// }

// return null

return trimmedPathname
}

Expand All @@ -82,7 +83,7 @@ export const findMatchPath = rawPathname => {
* @param {string} rawPathname A raw pathname
* @return {object}
*/
export const grabMatchParams = rawPathname => {
export const grabMatchParams = (rawPathname) => {
// const trimmedPathname = cleanPath(rawPathname)

// const pickPaths = matchPaths.map(({ path, matchPath }) => {
Expand All @@ -109,12 +110,17 @@ export const grabMatchParams = rawPathname => {
//
// Or if `match-paths.json` contains `{ "/foo*": "/page1", ...}`, then
// `/foo?bar=far` => `/page1`
export const findPath = rawPathname => {
export const findPath = (rawPathname) => {
const trimmedPathname = trimPathname(absolutify(rawPathname))
if (pathCache.has(trimmedPathname)) {
return pathCache.get(trimmedPathname)
}

const redirect = maybeGetBrowserRedirect(rawPathname)
if (redirect) {
return findPath(redirect.toPath)
}

let foundPath = findMatchPath(trimmedPathname)

if (!foundPath) {
Expand All @@ -133,7 +139,7 @@ export const findPath = rawPathname => {
* @param {string} rawPathname A raw pathname
* @return {string}
*/
export const cleanPath = rawPathname => {
export const cleanPath = (rawPathname) => {
const trimmedPathname = trimPathname(absolutify(rawPathname))

let foundPath = trimmedPathname
Expand Down
59 changes: 29 additions & 30 deletions packages/gatsby-plugin-performance/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,35 @@
* React 17's Concurrent Mode
*/

import { setZeroTimeout } from './zero-timeout'
import { setZeroTimeout } from './zero-timeout'

const scheduleTask = cb =>
new Promise(resolve => setZeroTimeout(() => resolve(cb())))
const scheduleTask = (cb) =>
new Promise((resolve) => setZeroTimeout(() => resolve(cb())))

scheduleTask(() => {
// evaluate React in a single task
require(`react-dom`)
})
.then(() =>
scheduleTask(() => {
// evaluate Gatsby dependencies
require(`@mikaelkristiansson/domready`)
require(`@gatsbyjs/reach-router`)
require(`gatsby-react-router-scroll`)
})
)
.then(() =>
scheduleTask(() => {
// evaluate Gatsby framework
require(`gatsby`)
})
)
.then(() =>
scheduleTask(() => {
const plugins = require(`./api-runner-browser-plugins`)
scheduleTask(() => {
// evaluate React in a single task
require(`react-dom`)
})
.then(() =>
scheduleTask(() => {
// evaluate Gatsby dependencies
require(`@gatsbyjs/reach-router`)
require(`gatsby-react-router-scroll`)
})
)
.then(() =>
scheduleTask(() => {
// evaluate Gatsby framework
require(`gatsby`)
})
)
.then(() =>
scheduleTask(() => {
const plugins = require(`./api-runner-browser-plugins`)

// Evaluate each plugin on its own task
return Promise.all(plugins.map(plugin => scheduleTask(plugin.plugin)))
})
)
// finally evaluate and run the app
.then(() => scheduleTask(() => require(`./production-app-upstream`)))
// Evaluate each plugin on its own task
return Promise.all(plugins.map((plugin) => scheduleTask(plugin.plugin)))
})
)
// finally evaluate and run the app
.then(() => scheduleTask(() => require(`./production-app-upstream`)))

0 comments on commit b18196a

Please sign in to comment.