1. 09 Jul, 2022 2 commits
  2. 08 Jul, 2022 17 commits
  3. 07 Jul, 2022 8 commits
  4. 06 Jul, 2022 13 commits
    • JJ Kasper's avatar
      v12.2.1-canary.4 · 34117946
      JJ Kasper authored
      34117946
    • Tim Neutkens's avatar
      Implement new client-side router (#37551) · f1131413
      Tim Neutkens authored
      ## Client-side router for `app` directory
      
      This PR implements the new router that leverages React 18 concurrent features like Suspense and startTransition.
      It also integrates with React Server Components and builds on top of it to allow server-centric routing that only renders the part of the page that has to change.
      
      It's one of the pieces of the implementation of https://nextjs.org/blog/layouts-rfc.
      
      ## Details
      
      I'm going to document the differences with the current router here (will be reworked for the upgrade guide)
      
      ### Client-side cache
      
      In the current router we have an in-memory cache for getStaticProps data so that if you prefetch and then navigate to a route that has been prefetched it'll be near-instant. For getServerSideProps the behavior is different, any navigation to a page with getServerSideProps fetches the data again.
      
      In the new model the cache is a fundamental piece, it's more granular than at the page level and is set up to ensure consistency across concurrent renders. It can also be invalidated at any level.
      
      #### Push/Replace (also applies to next/link)
      
      The new router still has a `router.push` / `router.replace` method.
      
      There are a few differences in how it works though:
      
      - It only takes `href` as an argument, historically you had to provide `href` (the page path) and `as` (the actual url path) to do dynamic routing. In later versions of Next.js this is no longer required and in the majority of cases `as` was no longer needed. In the new router there's no way to reason about `href` vs `as` because there is no notion of "pages" in the browser.
      - Both methods now use `startTransition`, you can wrap these in your own `startTransition` to get `isPending`
      - The push/replace support concurrent rendering. When a render is bailed by clicking a different link to navigate to a completely different page that still works and doesn't cause race conditions.
      - Support for optimistic loading states when navigating
      
      ##### Hard/Soft push/replace
      
      Because of the client-side cache being reworked this now allows us to cover two cases: hard push and soft push.
      
      The main difference between the two is if the cache is reused while navigating. The default for `next/link` is a `hard` push which means that the part of the cache affected by the navigation will be invalidated, e.g. if you already navigated to `/dashboard` and you `router.push('/dashboard')` again it'll get the latest version. This is similar to the existing `getServerSideProps` handling.
      
      In case of a soft push (API to be defined but for testing added `router.softPush('/')`) it'll reuse the existing cache and not invalidate parts that are already filled in. In practice this means it's more like the `getStaticProps` client-side navigation because it does not fetch on navigation except if a part of the page is missing.
      
      #### Back/Forward navigation
      
      Back and Forward navigation ([popstate](https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event)) are always handled as a soft navigation, meaning that the cache is reused, this ensures back/forward navigation is near-instant when it's in the client-side cache. This will also allow back/forward navigation to be a high priority update instead of a transition as it is based on user interaction. Note: in this PR it still uses `startTransition` as there's no way to handle the high priority update suspending which happens in case of missing data in the cache. We're working with the React team on a solution for this particular case.
      
      ### Layouts
      
      Note: this section assumes you've read [The layouts RFC](https://nextjs.org/blog/layouts-rfc) and [React Server Components RFC](https://reactjs.org/blog/2020/12/21/data-fetching-with-react-server-components.html)
      
      React Server Components rendering leverages the Flight streaming mechanism in React 18, this allows sending a serializable representation of the rendered React tree on the server to the browser, the client-side React can use this serialized representation to render components client-side without the JavaScript being sent to the browser. This is one of the building blocks of Server Components. This allows a bunch of interesting features but for now I'll keep it to how it affects layouts.
      
      When you have a `app/dashboard/layout.js` and `app/dashboard/page.js` the page will render as children of the layout, when you add another page like `app/dashboard/integrations/page.js` that page falls under the dashboard layout as well. When client-side navigating the new router automatically figures out if the page you're navigating to can be a smaller render than the whole page, in this case `app/dashboard/page.js` and `app/dashboard/integrations/page.js` share the `app/dashboard/layout.js` so instead of rendering the whole page we render below the layout component, this means the layout itself does not get re-rendered, the layout's `getServerSideProps` would not be called, and the Flight response would only hold the result of `app/dashboard/integrations/page.js`, effectively giving you the smallest patch for the UI.
      
      ---
      
      Note: the commits in this PR were mostly work in progress to ensure it wasn't lost along the way. The implementation was reworked a bunch of times to where it is now.
      Co-authored-by: 's avatarJiachi Liu <4800338+huozhi@users.noreply.github.com>
      Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
      f1131413
    • Damien Simonin Feugas's avatar
      feat: build edge functions with node.js modules and fail at runtime (#38234) · 6e2c3821
      Damien Simonin Feugas authored
      ## What's in there?
      
      The Edge runtime [does not support Node.js modules](https://edge-runtime.vercel.app/features/available-apis#unsupported-apis).
      When building Next.js application, we currently fail the build when detecting node.js module imported from middleware.
      
      This is an blocker for using code that is conditionally loading node.js modules (based on platform/env detection), as @cramforce reported.
      
      This PR implements a new strategy where:
      - we can build such middleware/Edge API route code **with a warning**
      - we fail at run time, with graceful errors in dev (console & react-dev-overlay error)
      - we fail at run time, with console errors in production
      
      ## How to test?
      
      All cases are covered with integration tests.
      To try them live, create a simple app with a page, a `middleware.js` file and a `pages/api/route.js`file.
      Here are iconic examples:
      
      ### node.js modules
      ```js
      // middleware.js
      import { NextResponse } from 'next/server'
      // static
      import { basename } from 'path'
      
      export default async function middleware() {
        // dynamic
        const { basename } = await import('path')
        basename()
        return NextResponse.next()
      }
      
      export const config = { matcher: '/' }
      ```
      ```js
      // pags/api/route.js
      // static
      import { isAbsolute } from 'path'
      
      export default async function handle() {
        // dynamic
        const { isAbsolute } = await import('path')
        return Response.json({ useNodeModule: isAbsolute('/test') })
      }
      
      export const config = { runtime: 'experimental-edge' }
      ```
      
      Desired error (+ source code highlight in dev):
      
      > The edge runtime does not support Node.js 'path' module
      Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
      
      Desired warning at build time:
      
      > A Node.js module is loaded ('path' at line 2) which is not supported in the Edge Runtime.
      Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
      
      - [x]  in dev middleware, static, shows desired error on stderr
      - [x]  in dev route, static, shows desired error on stderr
      - [x]  in dev middleware, dynamic, shows desired error on stderr
      - [x]  in dev route, dynamic, shows desired error on stderr
      - [x]  in dev middleware, static, shows desired error on react error overlay
      - [x]  in dev route, static, shows desired error on react error overlay
      - [x]  in dev middleware, dynamic, shows desired error on react error overlay
      - [x]  in dev route, dynamic, shows desired error on react error overlay
      - [x]  builds middleware successfully, shows build warning, shows desired error on stderr on call
      - [x]  builds route successfully, shows build warning, shows desired error on stderr on call
      
      ### 3rd party modules not found
      
      ```js
      // middleware.js
      import { NextResponse } from 'next/server'
      // static
      import Unknown from 'unknown'
      
      export default async function middleware() {
        // dynamic
        const Unknown = await import('unknown')
        new Unknown()
        return NextResponse.next()
      }
      ```
      export const config = { matcher: '/' }
      ```
      ```js
      // pags/api/route.js
      // static
      import Unknown from 'unknown'
      
      export default async function handle() {
        // dynamic
        const Unknown = await import('unknown')
        return Response.json({ use3rdPartyModule: Unknown() })
      }
      
      export const config = { runtime: 'experimental-edge' }
      ```
      
      Desired error (+ source code highlight in dev):
      
      > Module not found: Can't resolve 'does-not-exist'
      Learn More: https://nextjs.org/docs/messages/module-not-found
      
      - [x]  in dev middleware, static, shows desired error on stderr
      - [x]  in dev route, static, shows desired error on stderr
      - [x]  in dev middleware, dynamic, shows desired error on stderr
      - [x]  in dev route, dynamic, shows desired error on stderr
      - [x]  in dev middleware, static, shows desired error on react error overlay
      - [x]  in dev route, static, shows desired error on react error overlay
      - [x]  in dev middleware, dynamic, shows desired error on react error overlay
      - [x]  in dev route, dynamic, shows desired error on react error overlay
      - [x]  fails to build middleware, with desired error on stderr
      - [x]  fails to build route, with desired error on stderr
      
      ### unused node.js modules
      ```js
      // middleware.js
      import { NextResponse } from 'next/server'
      
      export default async function middleware() {
        if (process.exit) {
          const { basename } = await import('path')
          basename()
        }
        return NextResponse.next()
      }
      ```
      ```js
      // pags/api/route.js
      export default async function handle() {
        if (process.exit) {
          const { basename } = await import('path')
          basename()
        }
        return Response.json({ useNodeModule: false })
      }
      
      export const config = { runtime: 'experimental-edge' }
      ```
      
      Desired warning at build time:
      
      > A Node.js module is loaded ('path' at line 2) which is not supported in the Edge Runtime.
      Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
      
      - [x]  invoke middleware in dev with no error
      - [x]  invoke route in dev with no error
      - [x]  builds successfully, shows build warning, invoke middleware with no error
      - [x]  builds successfully, shows build warning, invoke api-route with no error
      
      ## Notes to reviewers
      
      The strategy to implement this feature is to leverages webpack [externals](https://webpack.js.org/configuration/externals/#externals) and run a global `__unsupported_module()` function when using a node.js module from edge function's code.
      For the record, I tried using [webpack resolve.fallback](https://webpack.js.org/configuration/resolve/#resolvefallback) and [Webpack.IgnorePlugin](https://webpack.js.org/plugins/ignore-plugin/) but they do not allow throwing proper errors at runtime that would contain the loaded module name for reporting.
      
      `__unsupported_module()` is defined in `EdgeRuntime`, and returns a proxy that's throw on use (whether it's property access, function call, new operator... synchronous & promise-based styles).
      
      However there's an issue with error reporting: webpack does not includes the import lines in the generated sourcemaps, preventing from displaying useful errors.
      I extended our middleware-plugin to supplement the sourcemaps (when analyzing edge function code, it saves which module is imported from which file, together with line/column/source)
      
      The react-dev-overlay was adapted to look for this additional information when the caught error relates to modules, instead of looking at sourcemaps.
      
      I removed the previous mechanism (built by @nkzawa ) which caught webpack errors at built time to change the displayed error message (files `next/build/index.js`, `next/build/utils.ts` and `wellknown-errors-plugin`)
      6e2c3821
    • Max Proske's avatar
      chore(examples): Convert `blog` example to TypeScript (#38095) · 0f4333a5
      Max Proske authored
      Converted Blog example over to TypeScript to match the Contribution guidelines.
      
      I also simplified the example by removing `_document.js` and prettier from `package.json`.
      
      ## Documentation / Examples
      
      - [X] Make sure the linting passes by running `pnpm lint`
      - [X] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
      0f4333a5
    • Zane Chua's avatar
      Update edge-runtime.md (#38271) · 3b3d249c
      Zane Chua authored
      * Update edge-runtime.md
      
      * Update docs/api-reference/edge-runtime.md
      
      * fix linting
      Co-authored-by: 's avatarLee Robinson <me@leerob.io>
      Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
      Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
      3b3d249c
    • Dustin Goodman's avatar
    • Marius Jørgensen's avatar
      Fix/update docs for swc styled components (#38280) · 3ae343fa
      Marius Jørgensen authored
      According to https://github.com/vercel/next.js/issues/30802 SWC transforms for `styled-components` were updated with support for `cssProp`, `fileName` and `namespace`. The docs only mention support for `ssr` and `displayName`.
      
      ## Documentation / Examples
      
      - [x] Make sure the linting passes by running `pnpm lint`
      3ae343fa
    • Rishabh Poddar's avatar
      Updates with-supertokens example (#38369) · b83107c3
      Rishabh Poddar authored
      * add-supertokens-to-authentication.md
      
      * bump supertokens deps
      
      * update SuperTokens Auth doc
      
      * Update docs/authentication.md
      Co-authored-by: 's avatarLee Robinson <me@leerob.io>
      
      * Update docs/authentication.md
      Co-authored-by: 's avatarLee Robinson <me@leerob.io>
      
      * updates supertokens dependency and optimises for serverless execution
      
      * runs prettier-fix
      
      * adds supertokens to 'Bring Your Own Database' section as well
      
      * does not show home page if not logged in
      
      * extracts config into its own file and calls it in all serverless functions
      
      * removes need for backend init in app.jsx
      
      * simplifies use of dynamic
      
      * refreshes page after getServerSideProps
      
      * removes unnecessary check in API
      
      * update to docs pertaining SuperTokens
      
      * adds placeholder secrets so that the UI loads on first run
      
      * changes to readme
      
      * updates version of supertokens frontend and backend SDK, and a few other fixes
      
      * Update docs/authentication.md
      Co-authored-by: 's avatarLee Robinson <me@leerob.io>
      
      * updates to readme for supertokens example
      
      * updates version of dependency
      
      * updates dependency version
      
      * updates to dependencies
      
      * removes unnecessary config on frontend
      
      * changes how redirection is done post signout
      
      * update to dependency
      
      * updates examples
      
      * updates code to use for new package
      
      * updates dependencies
      
      * updates auth-react package
      
      * with-supertokens example updated to use supertokens-node v7
      
      * updates dependency
      
      * updates supertokens-node version
      
      * Update examples/with-supertokens/package.json
      Co-authored-by: 's avatarLee Robinson <me@leerob.io>
      
      * updates based on check-examples.sh script
      
      * linter fix
      
      * updates supertokens-auth-react dependency version
      
      * adds development OAuth key to example
      
      * removes section from README
      
      * removes unnecessary file
      
      * updates dependency versions
      
      * with-supertokens: reduced bundle size by removing node lib from bundle
      
      Linting fix
      
      * Removed accidentally added config file
      
      * adds sign in with apple
      
      * extracted oauth keys to .env file
      
      * fixes node init issue race condition
      
      * removes unnecessary file
      
      * updates supertokens-auth-react dependency
      
      * updates superttokens-node dependency
      
      * adds a cap to react dependency
      
      * updates eslint-config-next version
      
      * removes unnecessary dev dependency
      
      * updates to latest version of supertokens-auth-react SDK
      
      * Updated nextjs in supertokens example
      
      * Update examples/with-supertokens/package.json
      
      * Update examples/with-supertokens/package.json
      
      * Update package.json
      
      * Update examples/with-supertokens/package.json
      Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
      
      * Update examples/with-supertokens/package.json
      Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
      
      * updates to supertokens-auth-react version
      
      * feat: update&improve ssr in with-supertokens
      
      * refactor: implement review feedback
      
      * refactor: moved everything into ProtectedPage to make Auth component usage clearer
      
      * refactor: implement review feedback
      
      * updates dependency version and uses nextjs router for navigation
      
      * removes prettier dendency in with-supertokens example app
      b83107c3
    • okmttdhr, okp's avatar
      Call Error.getInitialProps for the top level error (#21240) · 69f8024a
      okmttdhr, okp authored
      * fix: Call Error.getInitialProps to pass `err`
      
      * test: Call Error.getInitialPropsfoe the top level error
      
      * test: Update build output limit
      
      * test: Add tests for top level error
      
      * Revert "test: Call Error.getInitialPropsfoe the top level error"
      
      This reverts commit 81fa0c38c704ae6feb79a65eba316362bbebc939.
      
      * test: Fix failing test
      69f8024a
    • Gal Schlezinger's avatar
      [edge] enable edge compiler source maps by default (#38365) · 708688dd
      Gal Schlezinger authored
      since we no longer have a single compiler for browser & edge, we can enable
      source map generation by default for all edge functions. This would make it
      much easier to debug and understand what's happening when deploying to prod
      as the log statements will show us the actual code location instead of post
      bundling and minified location.
      
      This also removes the experimental flag as it's not needed anymore.
      708688dd
    • Shu Ding's avatar
      Add more CSS imports test cases (#38343) · f3969888
      Shu Ding authored
      
      
      ## Bug
      
      - [ ] Related issues linked using `fixes #number`
      - [x] Integration tests added
      - [ ] Errors have helpful link attached, see `contributing.md`
      
      ## Feature
      
      - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
      - [ ] Related issues linked using `fixes #number`
      - [ ] Integration tests added
      - [ ] Documentation added
      - [ ] Telemetry added. In case of a feature if it's used or not.
      - [ ] Errors have helpful link attached, see `contributing.md`
      
      ## Documentation / Examples
      
      - [ ] Make sure the linting passes by running `pnpm lint`
      - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
      f3969888
    • Jiachi Liu's avatar
      Filter proper chunks from chunk group for client components (#38379) · a16d8dd4
      Jiachi Liu authored
      Client components might result in a page chunk or a standalone splitted chunk marked in flight manifest, this PR filters out the page chunk for standalone chunk so that while loading a client chunk (like for `next/link`) it won't load the page chunk to break the hydration. `chunk.ids` is not enough for getting required chunks, so we get the chunks from chunk group then filter the required ones.
      
      tests: Re-enable few previous rsc tests
      chore: refactor few webpack api usage
      
      ## Bug
      
      - [ ] Related issues linked using `fixes #number`
      - [x] Integration tests added
      - [ ] Errors have helpful link attached, see `contributing.md`
      a16d8dd4
    • Maia Teegarden's avatar
      Enable swc minifier in create next apps (#38215) · f2b8a6b6
      Maia Teegarden authored
      
      
      ## Bug
      
      - [ ] Related issues linked using `fixes #number`
      - [ ] Integration tests added
      - [ ] Errors have helpful link attached, see `contributing.md`
      
      ## Feature
      
      - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
      - [ ] Related issues linked using `fixes #number`
      - [ ] Integration tests added
      - [ ] Documentation added
      - [ ] Telemetry added. In case of a feature if it's used or not.
      - [ ] Errors have helpful link attached, see `contributing.md`
      
      ## Documentation / Examples
      
      - [ ] Make sure the linting passes by running `pnpm lint`
      - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
      f2b8a6b6