-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby): don't serve codeframes for files outside of compilation (#…
…38059) * test: add test case for overlay handlers * fix: don't serve codeframes for files outside of compilation (cherry picked from commit ed5855e)
- Loading branch information
Showing
5 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
this file shouldn't be allowed to be served | ||
this file shouldn't be allowed to be served. CYPRESS-MARKER |
20 changes: 20 additions & 0 deletions
20
...development-runtime/cypress/integration/hot-reloading/error-handling/overlay-endpoints.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const cwd = Cypress.config(`projectRoot`) | ||
|
||
describe(`overlay handlers don't serve unrelated files`, () => { | ||
it(`__file-code-frame`, () => { | ||
cy.request( | ||
`__file-code-frame?filePath=${cwd}/SHOULD_NOT_SERVE&lineNumber=0` | ||
).should(response => { | ||
expect(response.body.codeFrame).not.to.match(/CYPRESS-MARKER/) | ||
}) | ||
}) | ||
|
||
it(`__original-stack-frame`, () => { | ||
cy.request( | ||
`__original-stack-frame?moduleId=${cwd}/SHOULD_NOT_SERVE&lineNumber=0&skipSourceMap=1` | ||
).should(response => { | ||
expect(response.body.codeFrame).not.to.match(/CYPRESS-MARKER/) | ||
expect(response.body.sourceContent).not.to.match(/CYPRESS-MARKER/) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/gatsby/src/utils/webpack/utils/is-file-inside-compilations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Compilation, NormalModule } from "webpack" | ||
|
||
const filesInsideDevelopHtmlCompilation = new Set<string>() | ||
|
||
function removeQueryParams(path: string): string { | ||
return path.split(`?`)[0] | ||
} | ||
|
||
export function setFilesFromDevelopHtmlCompilation( | ||
developHtmlCompilation: Compilation | ||
): void { | ||
filesInsideDevelopHtmlCompilation.clear() | ||
|
||
for (const module of developHtmlCompilation.modules) { | ||
if (module instanceof NormalModule && module.resource) { | ||
filesInsideDevelopHtmlCompilation.add(removeQueryParams(module.resource)) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a file is inside either `develop` or `develop-html` compilation. Used to determine if | ||
* we should generate codeframe for this file for error overlay. | ||
*/ | ||
export function isFileInsideCompilations( | ||
absolutePath: string, | ||
developBrowserCompilation: Compilation | ||
): boolean { | ||
if (filesInsideDevelopHtmlCompilation.has(absolutePath)) { | ||
return true | ||
} | ||
|
||
for (const module of developBrowserCompilation.modules) { | ||
if (module instanceof NormalModule && module.resource) { | ||
if (absolutePath === removeQueryParams(module.resource)) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} |