Skip to content

Commit

Permalink
fix(gatsby): prevent infinite loop in fast-refresh-overlay (gatsbyjs#…
Browse files Browse the repository at this point in the history
…31594)

Co-authored-by: Leo Mrav <udemy13579@protonmail.com>
  • Loading branch information
pindjur and leomrav authored May 26, 2021
1 parent ca28d54 commit 3368884
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions packages/gatsby/cache-dir/fast-refresh-overlay/components/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import * as React from "react"
import { prettifyStack } from "../utils"

const initialResponse = {
decoded: null,
sourcePosition: {
line: null,
number: null,
},
sourceContent: null,
}

export function useStackFrame({ moduleId, lineNumber, columnNumber }) {
const url =
`/__original-stack-frame?moduleId=` +
Expand All @@ -10,26 +19,26 @@ export function useStackFrame({ moduleId, lineNumber, columnNumber }) {
`&columnNumber=` +
window.encodeURIComponent(columnNumber)

const [response, setResponse] = React.useState({
decoded: null,
sourcePosition: {
line: null,
number: null,
},
sourceContent: null,
})
const [response, setResponse] = React.useState(initialResponse)

React.useEffect(() => {
async function fetchData() {
const res = await fetch(url)
const json = await res.json()
const decoded = prettifyStack(json.codeFrame)
const { sourcePosition, sourceContent } = json
setResponse({
decoded,
sourceContent,
sourcePosition,
})
try {
const res = await fetch(url)
const json = await res.json()
const decoded = prettifyStack(json.codeFrame)
const { sourcePosition, sourceContent } = json
setResponse({
decoded,
sourceContent,
sourcePosition,
})
} catch (err) {
setResponse({
...initialResponse,
decoded: prettifyStack(err.message),
})
}
}
fetchData()
}, [])
Expand Down

0 comments on commit 3368884

Please sign in to comment.