Unverified Commit 619c76c0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix infinite invalidations loop in app dir (#46526)Co-authored-by: kodiakhq[bot]…

Fix infinite invalidations loop in app dir (#46526)Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>

This fixes a case where depending on the timings of compilers being done
could cause an infinite invalidation loop.

**Before**


https://user-images.githubusercontent.com/22380829/221786587-1a4cc6ab-f273-4191-92af-a57e9fff1261.mp4

**After**


https://user-images.githubusercontent.com/22380829/221786611-f55c3da9-0201-40be-95a8-3ef1869d6a66.mp4

x-ref: [slack
thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1677441845988529?thread_ts=1677429424.151329&cid=C03KAR5DCKC)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

---------
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 5ff005f7
...@@ -215,8 +215,8 @@ class Invalidator { ...@@ -215,8 +215,8 @@ class Invalidator {
continue continue
} }
this.multiCompiler.compilers[COMPILER_INDEXES[key]].watching?.invalidate()
this.building.add(key) this.building.add(key)
this.multiCompiler.compilers[COMPILER_INDEXES[key]].watching?.invalidate()
} }
} }
...@@ -224,9 +224,9 @@ class Invalidator { ...@@ -224,9 +224,9 @@ class Invalidator {
this.building.add(compilerKey) this.building.add(compilerKey)
} }
public doneBuilding() { public doneBuilding(compilerKeys: typeof COMPILER_KEYS = []) {
const rebuild: typeof COMPILER_KEYS = [] const rebuild: typeof COMPILER_KEYS = []
for (const key of COMPILER_KEYS) { for (const key of compilerKeys) {
this.building.delete(key) this.building.delete(key)
if (this.rebuildAgain.has(key)) { if (this.rebuildAgain.has(key)) {
...@@ -452,10 +452,15 @@ export function onDemandEntryHandler({ ...@@ -452,10 +452,15 @@ export function onDemandEntryHandler({
return pagePaths return pagePaths
} }
for (const compiler of multiCompiler.compilers) {
compiler.hooks.done.tap('NextJsOnDemandEntries', () =>
getInvalidator().doneBuilding([
compiler.name as keyof typeof COMPILER_INDEXES,
])
)
}
multiCompiler.hooks.done.tap('NextJsOnDemandEntries', (multiStats) => { multiCompiler.hooks.done.tap('NextJsOnDemandEntries', (multiStats) => {
if (invalidator.shouldRebuildAll()) {
return invalidator.doneBuilding()
}
const [clientStats, serverStats, edgeServerStats] = multiStats.stats const [clientStats, serverStats, edgeServerStats] = multiStats.stats
const root = !!appDir const root = !!appDir
const pagePaths = [ const pagePaths = [
...@@ -492,7 +497,7 @@ export function onDemandEntryHandler({ ...@@ -492,7 +497,7 @@ export function onDemandEntryHandler({
doneCallbacks!.emit(page) doneCallbacks!.emit(page)
} }
invalidator.doneBuilding() invalidator.doneBuilding([...COMPILER_KEYS])
}) })
const pingIntervalTime = Math.max(1000, Math.min(5000, maxInactiveAge)) const pingIntervalTime = Math.max(1000, Math.min(5000, maxInactiveAge))
......
import { createNextDescribe, FileRef } from 'e2e-utils'
import { check, hasRedbox, waitFor } from 'next-test-utils'
import path from 'path'
createNextDescribe(
'Error overlay - RSC build errors',
{
files: new FileRef(path.join(__dirname, 'fixtures', 'app-hmr-changes')),
dependencies: {
'@next/mdx': 'canary',
'react-wrap-balancer': '^0.2.4',
'next-tweet': '^0.6.0',
'@mdx-js/react': '^2.3.0',
tailwindcss: '^3.2.6',
typescript: 'latest',
'@types/react': '^18.0.28',
'@types/react-dom': '^18.0.10',
'image-size': '^1.0.2',
autoprefixer: '^10.4.13',
},
},
({ next }) => {
it('should handle successive HMR changes with errors correctly', async () => {
const browser = await next.browser('/2020/develop-preview-test')
await check(
() => browser.eval('document.documentElement.innerHTML'),
/A few years ago I tweeted/
)
const pagePath = 'app/(post)/2020/develop-preview-test/page.mdx'
const originalPage = await next.readFile(pagePath)
const break1 = originalPage.replace('break 1', '<Figure>')
await next.patchFile(pagePath, break1)
const break2 = break1.replace('{/* break point 2 */}', '<Figure />')
await next.patchFile(pagePath, break2)
for (let i = 0; i < 5; i++) {
await next.patchFile(pagePath, break2.replace('break 3', '<Hello />'))
await next.patchFile(pagePath, break2)
expect(await hasRedbox(browser, true)).toBe(true)
await next.patchFile(pagePath, break1)
await waitFor(100)
await next.patchFile(pagePath, originalPage)
expect(await hasRedbox(browser, false)).toBe(false)
}
await check(
() => browser.eval('document.documentElement.innerHTML'),
/A few years ago I tweeted/
)
})
}
)
A few years ago I tweeted a simple guideline that
[challenged conventional wisdom](https://twitter.com/swyx/status/1261202288476971008)
and became the norm for many people learning
[how to test their frontends](https://kentcdodds.com/blog/write-tests)
effectively.
Writing tests is an **investment**, and like any other investment,
it has to be evaluated in terms of its **return** and **risks** (e.g.:
[opportunity costs](https://en.wikipedia.org/wiki/Opportunity_cost)) it incurs in.
The "not too many" keys in on that. There is, after all, too much of a
good thing when it comes to spending time writing tests instead of
features. We can see an application of this insight also to availability
by [Google's popular SRE book](https://landing.google.com/sre/sre-book/chapters/embracing-risk/):
> You might expect Google to try to build 100% reliable services—ones that
> never fail. It turns out that past a certain point, however, increasing
> reliability is worse for a service (and its users) rather than better!
> Extreme reliability comes at a cost: maximizing stability limits how fast
> new features can be developed and how quickly products can be delivered to
> users, and dramatically increases their cost, which in turn reduces the
> numbers of features a team can afford to offer
In this essay I want to make the case that
**prioritizing end-to-end (E2E) testing** for the critical parts of
your app will reduce risk and give you the best return. Further, I'll show
how you can adopt this methodology in mere minutes.
## Why end-to-end?
In addition to integration tests, I want to now make the case that modern
deployment workflows in combination with serverless testing solutions can
now fully revert the conventional testing pyramid:
<Image
alt={
<span>
Martin Fowler's conventional{' '}
<a
href="https://martinfowler.com/articles/practical-test-pyramid.html#TheTestPyramid"
target="_blank"
>
testing pyramid
</a>
. What if 🐢 and 💲 went away?
</span>
}
src="/images/develop-preview-test/pyramid.jpg"
/>
As it turns out,
[focusing on the customer first](https://blog.aboutamazon.com/company-news/2016-letter-to-shareholders)
is the best way to run a business, but also the best way to ascertain
software quality.
You might be using the fanciest new compiler, the newest type system, but
won't do well if, after you ship, your site doesn't load in Chrome at all
because you forgot to send the right HTTP headers for your `.js` files.
Or you use features not compatible with Safari.
Modern cloud software has a great deal of complexity that cannot be
ascertained "in the lab", or by running artificial tests in your computer.
We normally deploy software to dozens of operating systems, mobile
devices, web browsers, networks of varying stability and performance… And
the environment is always changing, as we take more and more dependencies
on hosted services and third-party APIs.
Furthermore, software quality goes beyond correctness. It's not just about
doing "the right thing".
[Quality encapsulates reliability](https://twitter.com/garybernhardt/status/1007699924866093056)
(whether your program stays working correctly over time) and
[performance](https://craigmod.com/essays/fast_software/)
(it must do the right thing quickly).
## End-to-end made possible
First, back when I argued for focusing on integration in 2016 and made no
mention of end-to-end tests, we didn't yet have the deployment
infrastructure we have today.
With [Vercel](https://vercel.com/),
every push to git gets deployed and given an URL. This can be done cost
effectively by focusing on incremental computation, where the underlying
static files and serverless functions used to power a deployment are
mostly re-used from deploy to deploy.
<Image
alt={
<span>
An example of a PR automatically deployed{' '}
<a
target="_blank"
href="https://github.com/rauchg/blog/pull/35#issuecomment-570270061"
>
for this very blog
</a>
</span>
}
src="/images/develop-preview-test/github-comment.jpg"
/>
Just like GitHub is capable of keeping the history of your code forever,
so can Vercel. Because deploys are
[stateless](/2020/2019-in-review#static-is-the-new-dynamic),
the resources are only used on-demand and scale infinitely with traffic.
That gets rid of an obvious initial objection: that it would be difficult
or costly to perform end-to-end tests against a live individualized
version of your website. Instead of thinking about a single staging server
that end-to-end tests are run against, now you can have millions of
concurrent "staging" environments.
I say "staging" in quotes because in reality, you run tests against the
real production infrastructure, which has enormous reliability benefits.
Not only are certain
[features these days only enabled when SSL is on](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts/features_restricted_to_secure_contexts),
but having every other production feature enabled, like brotli
compression and HTTP/2, reduces your risk of outages:
<Tweet
id="921185541487341568"
caption="The risks of environment differences, as explained by Kubernetes' creator"
/>
Having made staging instant, prod-like and cost-effective, the remaining
objection is that running the actual end-to-end tests would be expensive
or slow, going by the pyramid above.
However, the introduction of
[puppeteer](https://github.com/puppeteer/puppeteer),
Chrome's headless web browser, combined with serverless compute that can
instantly scale to thousands of concurrent executions (see this
[Stanford paper](https://stanford.edu/~sadjad/gg-paper.pdf)
for an example) are <b>now making E2E both fast and cheap</b>.
## End-to-end made easy
To show the **Develop -> Preview -> Test** workflow in action, I'm
going to demonstrate how easily I can add an E2E test for
[my open-source blog](https://github.com/rauchg/blog)
using [Vercel](https://vercel.com) in combination with
[Checkly](https://www.checklyhq.com)
on top of GitHub.
First, I'm going to define an end-to-end test to ascertain that my blog
always contains the string "Guillermo Rauch" in the browser title. As I'm
writing this, it's not the case, so I expect this to fail.
Checkly gives me full access to the Node and puppeteer APIs. Notice I
don't have to write any ceremony, setup or scheduling logic. I'm "inside"
the function that will later be invoked:
<Image
alt="The ENVIRONMENT_URL env variable will be populated with each preview deploy URL"
src="/images/develop-preview-test/checkly.jpg"
/>
Then, I installed the
[Checkly GitHub app](https://www.checklyhq.com/docs/cicd/github/#setting-up-your-github-integration)
and under the CI/CD tab of the check, I linked it to my
`rauchg/blog` repository.
I created a git branch where I introduced the `<title>`
tag for my blog, but on purpose
[I misspelt my name](https://github.com/rauchg/blog/pull/53/commits/b5c30eaef41944f36cf14e7d7f8be9be9953709f).
As soon as I created my pull request, the check was already failing. In
just a few seconds, I pushed, deployed, a headless browser simulating a
visitor ran, and my error was exposed:
break 1
<Image
alt="I can also configure my testing check as mandatory and make this PR unmergeable"
src="/images/develop-preview-test/failing-check.jpg"
/>
break 2
break 3
This happened with absolutely zero additional configuration. Vercel
supplied a URL, Checkly tested it, GitHub was notified. After pushing
again, the check re-runs automatically:
<Image
alt="Each commit gets its own deploy preview, and its own checks"
src="/images/develop-preview-test/commits.jpg"
/>
With my typo fixed, I'm free to merge. Merging to master will deploy my
changes to `rauchg.com` automatically.
<Image
alt="After pressing the green button, we go live. With confidence."
src="/images/develop-preview-test/github-green.jpg"
/>
## Conclusion
Notably, Checkly allows us to configure multiple locations in the world
that the tests get executed from, as well as
<b>invoking our checks over time</b>.
Leslie Lamport
[famously said](https://lamport.azurewebsites.net/pubs/distributed-system.txt)
that a distributed system is one where "the failure of a computer you
didn't even know existed can render your own computer unusable".
As our systems become more complex, with ever-changing platforms and
dependencies on other cloud systems, continuously testing just like our
users do is our best bet to tame the chaos.
import Link from 'next/link'
export function A({ children, className = '', href, ...props }) {
return (
<Link
href={href}
className={`border-b text-gray-600 border-gray-300 transition-[border-color] hover:border-gray-600 dark:text-white dark:border-gray-500 dark:hover:border-white ${className}`}
{...props}
>
{children}
</Link>
)
}
import type { ReactNode } from 'react'
export function Blockquote({ children }: { children: ReactNode }) {
return (
<blockquote className="my-5 text-gray-500 pl-3 border-l-4 dark:border-gray-600 dark:text-gray-400">
{children}
</blockquote>
)
}
export const Callout = ({ emoji = null, text }) => (
<div className="bg-gray-200 dark:bg-[#333] dark:text-gray-300 flex items-start p-3 my-6 text-base">
<span className="block w-6 text-center text-xl mr-2">{emoji}</span>
<span className="block grow">{text}</span>
</div>
)
import Balancer from 'react-wrap-balancer'
import type { ReactNode } from 'react'
export function Caption({ children }: { children: ReactNode }) {
return (
<p className="text-xs my-3 font-mono text-gray-500 text-center leading-normal">
<Balancer>
<span className="[&>a]:post-link">{children}</span>
</Balancer>
</p>
)
}
import { A } from './a'
import { P } from './p'
export const FootNotes = ({ children }) => (
<div className="text-base before:w-[200px] before:m-auto before:content[''] before:border-t before:border-gray-300 dark:before:border-[#444] before:block before:my-10">
{children}
</div>
)
export const Ref = ({ id }) => (
<a
href={`#f${id}`}
id={`s${id}`}
className="relative text-xs top-[-5px] no-underline"
>
[{id}]
</a>
)
export const FootNote = ({ id, children }) => (
<P>
{id}.{' '}
<A href={`#s${id}`} id={`f${id}`} className="no-underline">
^
</A>{' '}
{children}
</P>
)
export function H1({ children }) {
return (
<h1 className="text-2xl font-bold mb-1 dark:text-gray-100">{children}</h1>
)
}
export function H2({ children }) {
return <h2 className="group font-bold text-xl my-8 relative">{children}</h2>
}
export function H3({ children }) {
return <h3 className="group font-bold text-lg my-8 relative">{children}</h3>
}
export function HR() {
return (
<div className="my-8 text-center after:content-['﹡﹡﹡'] after:text-sm after:text-center after:inline" />
)
}
import sizeOf from 'image-size'
import { join } from 'path'
import { readFile } from 'fs/promises'
import NextImage from 'next/image'
export async function Image({ src, alt = null, width = null, height = null }) {
if (!src.startsWith('data:') && (width === null || height === null)) {
let imageBuffer = null
if (src.startsWith('http')) {
imageBuffer = Buffer.from(
await fetch(src).then((res) => res.arrayBuffer())
)
} else {
imageBuffer = await readFile(
new URL(join(import.meta.url, '..', '..', '..', '..', 'public', src))
.pathname
)
}
;({ width, height } = sizeOf(imageBuffer))
}
return (
<span className="my-5 flex flex-col items-center">
{src.startsWith('data:') ? (
<img src={src} alt={alt} />
) : (
<NextImage width={width} height={height} alt={alt} src={src} />
)}
{alt && (
<span className="block font-mono text-xs mt-5 text-center">{alt}</span>
)}
</span>
)
}
// we use `[ul_&]` prefix for the <UL> variety
export function LI({ children }) {
return (
<li
className={`
my-2
[ul_&]:relative
[ul_&]:pl-4
[ul_&]:before:text-gray-400
[ul_&]:before:content-['–']
[ul_&]:before:mr-2
[ul_&]:before:absolute
[ul_&]:before:-ml-4
`}
>
{children}
</li>
)
}
export function OL({ children }) {
return <ol className="my-5 list-decimal list-inside">{children}</ol>
}
export function P({ children }) {
return <p className="my-5 [blockquote_&]:my-2">{children}</p>
}
import { Caption } from './caption'
export const Snippet = ({ children, scroll = true, caption = null }) => (
<div className="my-6">
<pre
className={`
p-4
text-sm
bg-gray-800 text-white
dark:bg-[#222] dark:text-gray-300
${
scroll
? 'overflow-scroll'
: 'whitespace-pre-wrap break-all overflow-hidden'
}
`}
>
<code>{children}</code>
</pre>
{caption != null ? <Caption>{caption}</Caption> : null}
</div>
)
import { NextTweet } from 'next-tweet'
import { Caption } from './caption'
import type { ReactNode } from 'react'
// we import this globally in the top-most layout.tsx file
// until Next.js lands suspense-y CSS support
// import "./tweet.css";
interface TweetArgs {
id: string
caption: ReactNode
}
export async function Tweet({ id, caption }: TweetArgs) {
return (
<div className="my-6">
<div className="flex justify-center">
{/* @ts-ignore */}
<NextTweet id={id} />
</div>
{caption && <Caption>{caption}</Caption>}
</div>
)
}
export function UL({ children }) {
return <ul className="my-5 list-none list-inside">{children}</ul>
}
export default function Root({ children }) {
return (
<html>
<head />
<body>{children}</body>
</html>
)
}
import { A as a } from 'app/(post)/components/a'
import { P as p } from 'app/(post)/components/p'
import { H1 as h1 } from 'app/(post)/components/h1'
import { H2 as h2 } from 'app/(post)/components/h2'
import { H3 as h3 } from 'app/(post)/components/h3'
import { OL as ol } from 'app/(post)/components/ol'
import { UL as ul } from 'app/(post)/components/ul'
import { LI as li } from 'app/(post)/components/li'
import { HR as hr } from 'app/(post)/components/hr'
import { Tweet } from 'app/(post)/components/tweet'
import { Image } from 'app/(post)/components/image'
import { Snippet } from 'app/(post)/components/snippet'
import { Caption } from 'app/(post)/components/caption'
import { Callout } from 'app/(post)/components/callout'
import { Ref, FootNotes, FootNote } from 'app/(post)/components/footnotes'
import { Blockquote as blockquote } from 'app/(post)/components/blockquote'
export function useMDXComponents(components: {
[component: string]: React.ComponentType
}) {
return {
...components,
a,
h1,
h2,
h3,
p,
ol,
ul,
li,
hr,
pre: Snippet,
img: Image,
blockquote,
Tweet,
Image,
Snippet,
Caption,
Callout,
Ref,
FootNotes,
FootNote,
}
}
const withMDX = require('@next/mdx')()
module.exports = withMDX({
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
experimental: {
appDir: true,
mdxRs: true,
},
images: {
domains: [
'pbs.twimg.com',
'abs.twimg.com',
'm.media-amazon.com',
'images-na.ssl-images-amazon.com',
],
},
})
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
const plugin = require('tailwindcss/plugin')
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: 'class',
content: ['./app/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {},
},
plugins: [
plugin(function ({ addVariant }) {
// this class is applied to `html` by `app/theme-efect.ts`, similar
// to how `dark:` gets enabled
addVariant('theme-system', '.theme-system &')
}),
],
future: {
hoverOnlyWhenSupported: true,
},
}
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"jsx": "preserve",
"strict": false,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"noEmit": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"incremental": true,
"baseUrl": "."
},
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment