-
Notifications
You must be signed in to change notification settings - Fork 86
fix: don't ever execute middleware in lambda #2249
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,34 @@ | ||
describe('Preview Mode', () => { | ||
it('enters and exits preview mode', () => { | ||
Cypress.Cookies.debug(true) | ||
cy.getCookies().then((cookie) => cy.log('cookies', cookie)) | ||
|
||
cy.intercept('/previewTest', (req) => { | ||
req.continue((res) => { | ||
expect(res.headers?.['x-middleware-executed']).to.equal('true') | ||
}) | ||
}).as('previewTestVisit') | ||
|
||
// preview mode is off by default | ||
cy.visit('/previewTest') | ||
cy.findByText('Is preview? No', { selector: 'h1' }) | ||
|
||
// enter preview mode | ||
cy.request('/api/enterPreview').then((response) => { | ||
expect(response.body).to.have.property('name', 'preview mode') | ||
}) | ||
|
||
// exptected content is rendered | ||
cy.visit('/previewTest') | ||
cy.findByText('Is preview? Yes!', { selector: 'h1' }) | ||
|
||
// exit preview mode | ||
cy.request('/api/exitPreview') | ||
cy.visit('/previewTest') | ||
cy.findByText('Is preview? No', { selector: 'h1' }) | ||
|
||
// we should hit /previewTest 3 times (before entering preview, after entering preview, after exiting preview) | ||
// this assertion is mainly to ensure interception works and assertion on response header is made | ||
cy.get('@previewTestVisit.all').should('have.length', 3) | ||
}) | ||
}) | ||
This file contains hidden or 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 hidden or 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,6 @@ | ||
export default async function preview(req, res) { | ||
// Enable Preview Mode by setting the cookies | ||
res.setPreviewData({}) | ||
|
||
res.status(200).json({ name: 'preview mode' }) | ||
} |
This file contains hidden or 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,8 @@ | ||
export default async function exit(_, res) { | ||
// Exit the current user from "Preview Mode". This function accepts no args. | ||
res.clearPreviewData() | ||
|
||
// Redirect the user back to the index page. | ||
res.writeHead(307, { Location: '/' }) | ||
res.end() | ||
} |
This file contains hidden or 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 hidden or 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,25 @@ | ||
import Link from 'next/link' | ||
|
||
const StaticTest = ({ isPreview }) => { | ||
return ( | ||
<div> | ||
<h1>Is preview? {isPreview ? 'Yes!' : 'No'}</h1> | ||
<p> | ||
<a href={isPreview ? '/api/exitPreview' : '/api/enterPreview'}> | ||
{isPreview ? 'Exit Preview' : 'Enter Preview'} | ||
</a> | ||
</p> | ||
<Link href="/">Go back home</Link> | ||
</div> | ||
) | ||
} | ||
|
||
export const getStaticProps = async ({ preview }) => { | ||
return { | ||
props: { | ||
isPreview: Boolean(preview), | ||
}, | ||
} | ||
} | ||
|
||
export default StaticTest |
This file contains hidden or 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ interface NetlifyConfig { | |
revalidateToken?: string | ||
} | ||
|
||
// eslint-disable-next-line max-lines-per-function | ||
const getNetlifyNextServer = (NextServer: NextServerType) => { | ||
class NetlifyNextServer extends NextServer { | ||
private netlifyConfig: NetlifyConfig | ||
|
@@ -118,6 +119,13 @@ const getNetlifyNextServer = (NextServer: NextServerType) => { | |
} | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this, require-await | ||
async runMiddleware(): Promise<{ finished: boolean }> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just linking to Next.js server's implementation for reference. https://github.com/vercel/next.js/blob/1286e145b058538e47b11cc1ec813a686cee1f68/packages/next/src/server/next-server.ts#L1474-L1589 |
||
return { | ||
finished: false, | ||
} | ||
} | ||
|
||
private getNetlifyPathsForRoute(route: string): string[] { | ||
const { i18n } = this.nextConfig | ||
const { routes, dynamicRoutes } = this.netlifyPrerenderManifest | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test does assert things correctly, but the version of Next used in
middleware
demo actually doesn't have the bug included, so the fix is only relevant to older next versions which we don't run test against currently :/I think it's still worthwhile to include the test, but we should figure out test setup to run against more versions of next.js.
[email protected]
does fail this test without the fix ( https://cloud.cypress.io/projects/yn8qwi/runs/2299/overview?roarHideRunsWithDiffGroupsAndTags=1 ) but with setup as-is we won't be using that version of next :/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good to have this like you mentioned and if we get around to running these tests on older versions, this will definitely be helpful.