Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): Convert redirects-writer to typescript #22530

Merged
merged 3 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ process.on(`unhandledRejection`, (reason, p) => {
import { createGraphQLRunner } from "./create-graphql-runner"
const { extractQueries } = require(`../query/query-watcher`)
const requiresWriter = require(`./requires-writer`)
const { writeRedirects } = require(`./redirects-writer`)
import { writeRedirects, startRedirectListener } from "./redirects-writer"

// Override console.log to add the source file + line number.
// Useful for debugging if you lose a console.log somewhere.
Expand Down Expand Up @@ -70,6 +70,8 @@ module.exports = async (args: BootstrapArgs) => {
// and invokes Gatsby API based on actions.
startPluginRunner()

startRedirectListener()

const directory = slash(args.directory)

const program = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import _ from "lodash"
import crypto from "crypto"
import fs from "fs-extra"
import { store, emitter } from "../redux/"
import { store, emitter } from "../redux"
import { joinPath } from "gatsby-core-utils"

let lastHash = null
let lastHash: string | null = null
let bootstrapFinished = false

const writeRedirects = async () => {
export const writeRedirects = async (): Promise<void> => {
bootstrapFinished = true

let { program, redirects } = store.getState()
const { program, redirects } = store.getState()

// Filter for redirects that are meant for the browser.
const browserRedirects = redirects.filter(r => r.redirectInBrowser)
Expand All @@ -31,16 +32,15 @@ const writeRedirects = async () => {
)
}

exports.writeRedirects = writeRedirects

let bootstrapFinished = false
const debouncedWriteRedirects = _.debounce(() => {
// Don't write redirects again until bootstrap has finished.
if (bootstrapFinished) {
writeRedirects()
}
}, 250)

emitter.on(`CREATE_REDIRECT`, () => {
debouncedWriteRedirects()
})
export const startRedirectListener = (): void => {
emitter.on(`CREATE_REDIRECT`, () => {
debouncedWriteRedirects()
})
}