1. 06 Mar, 2024 6 commits
  2. 04 Mar, 2024 7 commits
    • JJ Kasper's avatar
      v14.1.2 · f564deef
      JJ Kasper authored
      f564deef
    • Jiachi Liu's avatar
      Fix sitemap generateSitemaps support for string id (#61088) · a85519e7
      Jiachi Liu authored
      Fixes the string id that broken when sitemap is optimized to static
      route.
      
      When sitemap is optimized to static route in production, the route
      argument is changed from `[[...__metadata_id__]]` to
      `[__metadata_id__]`, so the type of it is also changed from array to
      string that should reflect in the loader code.
      
      Fixes #60894
      Closes NEXT-2154
      a85519e7
    • Abhinay Pandey's avatar
      Fix: generateSitemaps in production giving 404 (#62212) · 0a2d7754
      Abhinay Pandey authored
      generateSitemaps function returns a 404 for /sitemap/[id].xml in
      production
      
      While finding the correct sitemap partition from the array, we check the
      param against the id. Which works in dev because id and param are both
      without trailing .xml. But it fails in production as param has a
      trailing .xml (/sitemap/[id] works in production because it falls back
      to dynamic loading and param and id are both without .xml)
      
      If we are in production environment, check the id with a trailing .xml
      because that's whats returned from generateStaticParams, an array of
      __metadata_id__ with trailing .xml
      
      Fixes #61969
      
      ---------
      Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
      0a2d7754
    • Jiachi Liu's avatar
      Fix redirect under suspense boundary with basePath (#62597) · 400b57cf
      Jiachi Liu authored
      Fixes `redirect()` call under suspense boundary, redirect url should
      include the `basePath` in the url.
      
      `redirect()` under suspense boundaries will go through server html
      insertion, which is being used every time by suspense boundary resolved,
      new errors will triggered from SSR streaming. It was missing before.
      
      Fixes NEXT-2615
      Fixes #62407
      400b57cf
    • Shu Ding's avatar
      fix: Add stricter check for "use server" exports (#62821) · 714020fb
      Shu Ding authored
      As mentioned in the new-added error messages, and the [linked
      resources](https://react.dev/reference/react/use-server#:~:text=Because%20the%20underlying%20network%20calls%20are%20always%20asynchronous%2C%20%27use%20server%27%20can%20only%20be%20used%20on%20async%20functions.):
      
      > Because the underlying network calls are always asynchronous, 'use
      server' can only be used on async functions.
      > https://react.dev/reference/react/use-server
      
      It's a requirement that only async functions are allowed to be exported
      and annotated with `'use server'`. Currently, we already have compiler
      check so this will already error:
      
      ```js
      'use server'
      
      export function foo () {} // missing async
      ```
      
      However, since exported values can be very dynamic the compiler can't
      catch all mistakes like that. We also have a runtime check for all
      exports in a `'use server'` function, but it only covers `typeof value
      === 'function'`.
      
      This PR adds a stricter check for "use server" annotated values to also
      make sure they're async functions (`value.constructor.name ===
      'AsyncFunction'`).
      
      That said, there are still cases like synchronously returning a promise
      to make a function "async", but it's still very different by definition.
      For example:
      
      ```js
      const f = async () => { throw 1; return 1 }
      const g = () => { throw 1; return Promise.resolve(1) }
      ```
      
      Where `g()` can be synchronously caught (`try { g() } catch {}`) but
      `f()` can't even if they have the same types. If we allow `g` to be a
      Server Action, this behavior is no longer always true but depending on
      where it's called (server or client).
      
      Closes #62727.
      714020fb
    • Zack Tanner's avatar
      ensure server action errors notify rejection handlers (#61588) · dbab205b
      Zack Tanner authored
      ### What
      When attaching a rejection listener to a server action promise, in the case of network errors, the rejection handler would be skipped and it'd throw an error that crashes the application.
      
      ### Why
      When we refactored these reducers to no longer suspend, it caused the rejection handling logic we have to no longer make sense. In this case we're working with a native promise that won't have a `status` property, so we'd re-throw the error and not call `reject`. 
      
      ### How
      This removes the special status handling logic and makes the rejection handler always call `reject` with the error. This will either be handled by user code or let the error bubble to an error boundary. 
      
      I also cleaned up some mutable code that is no longer needed now that these reducers aren't replayed. 
      
      Closes NEXT-1715
      Closes NEXT-2323
      Fixes #58638
      dbab205b
    • Zack Tanner's avatar
      make router restore action resilient to a missing tree (#62098) · a70ae409
      Zack Tanner authored
      Following an anchor link to a hash param, and then attempting to use
      `history.pushState` or `history.replaceState`, would result in an MPA
      navigation to the targeted URL.
      
      In #61822, a guard was added to prevent calling `ACTION_RESTORE` with a
      missing tree, to match other call-sites where we do the same. This was
      to prevent the app from crashing in the case where app router internals
      weren't available in the history state. The original assumption was that
      this is a rare / unlikely edge case. However the above scenario is a
      very probable case where this can happen, and triggering an MPA
      navigation isn't ideal.
      
      This updates `ACTION_RESTORE` to be resilient to an undefined router
      state tree. When this happens, we'll still trigger the restore action to
      sync params, but use the existing flight router state.
      
      Closes NEXT-2502
      a70ae409
  3. 02 Mar, 2024 3 commits
  4. 29 Feb, 2024 5 commits
  5. 28 Feb, 2024 19 commits
    • Max Leiter's avatar
      Server Action tests (#62655) · 74b3f0f4
      Max Leiter authored
      ### What?
      Backports some Server Action changes from Shu to 14.1.1.
      
      
      [Improve the Server Actions SWC transform
      ](https://github.com/vercel/next.js/pull/62655/commits/bf6460e985c0ff8e41c03e000cbcb2638bcf99c7)
      
      [Fix Server Reference being double
      registered](https://github.com/vercel/next.js/pull/62655/commits/ef81b1099856bc7918dfa39e6d272cce53451eaf)
      
      
      [Improve the Server Actions SWC transform (part
      2)](https://github.com/vercel/next.js/pull/62655/commits/6e59c222a84f48edfc958d3327eb5448431e7784)
      
      [Fix module-level Server Action creation with closure-closed
      values](https://github.com/vercel/next.js/pull/62655/commits/51c6a07ec6a373eca3425ef565a53f4105112099)
      
      
      [Rename internal utility naming for
      clarification](https://github.com/vercel/next.js/pull/62655/commits/00d8e6e0612b7afddbce5f944a7330413e548871)
      
      ### Why?
      
      ### How?
      
      ---------
      Co-authored-by: 's avatarShu Ding <g@shud.in>
      Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
      74b3f0f4
    • Jiachi Liu's avatar
      Backport metadata fixes (#62663) · a6946b69
      Jiachi Liu authored
      Backport metadata fixes 
      #61898
      #62109
      #62615
      
      Closes NEXT-2641
      a6946b69
    • Ethan Arrowood's avatar
      Fix draft mode invariant (#62121) · 4002f4b3
      Ethan Arrowood authored
      ### What?
      
      In today's implementation, requests that don't end in `.rsc` are getting
      the data request query. We need to remove this toggle to prevent them
      from 500'ing.
      
      ### Why?
      
      The invariant triggered was:
      
      
      https://github.com/vercel/next.js/blob/212553958c671ea1f71d96fbc97ea4b9e5019bf3/packages/next/src/server/base-server.ts#L2838-L2842
      
      Crawling up the code, the conditional:
      
      
      https://github.com/vercel/next.js/blob/212553958c671ea1f71d96fbc97ea4b9e5019bf3/packages/next/src/server/base-server.ts#L2815
      
      must be `true`.
      
      The variable `isDataReq` is set here:
      
      
      https://github.com/vercel/next.js/blob/212553958c671ea1f71d96fbc97ea4b9e5019bf3/packages/next/src/server/base-server.ts#L1833-L1839
      
      This conditional expression is a bit complex, but it simplifies down to:
      - `isDataReq` is `true` when (TODO)
      - `isDataReq` is `false` when (TODO)
      
      ### How?
      
      Closes NEXT-2341
      Fixes #61377
      
      Tested manually by:
      - Building and packaging this branch locally
      - Deploying tarball and then depending on it as the Next.js version for
      an example app similar to the one reported by #61377
      - Inspecting console for 500 errors.
      
      ---------
      Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
      Co-authored-by: 's avatarZack Tanner <zacktanner@gmail.com>
      4002f4b3
    • Steven's avatar
      fix: babel usage with next/image (#61835) · 7dbf6f82
      Steven authored
      The `process/browser` lib was imported and being transformed, but
      `process.browser = true` turns to `true = true` that causes the
      compilation error, we need to exclude them from certain compilation
      layers
      
      - Fixes https://github.com/vercel/next.js/issues/61116
      
      Closes NEXT-2410
      Closes NEXT-2328
      
      ---------
      Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
      7dbf6f82
    • Jiachi Liu's avatar
      Fix next/server apit push alias for ESM pkg (#61721) · 3efc842a
      Jiachi Liu authored
      We have a modularize imports config for `next/server` before, which will
      transform the `next/server` imports to directly import from the actual
      file, for instance: `import { NextRequest } from 'next/server'` will
      become `import { NextRequest } from
      'next/dist/server/web/exports/next-request'`, where the NextRequest is
      exported as default export. This is fine in most case until you're using
      a ESM pkg, then it will be resolved as `{ default: NextRequest }`
      according to the spec. Since it's a ESM import to a CJS module in
      `next/dist`.
      
      Since we already have the ESM alias introduced in #59852 , this can
      handle the case more properly.
      
      Remove the modularize imports config for `next/server`, use the ESM api
      alias instead.
      
      Migrate the cjs optimizer tests from middleware to a separate endpoint
      `/cjs/server`. As now ESM imports for next/server are not going to get
      tree-shaken in dev, but since we don't have image response there it's
      still fine.
      
      Closes NEXT-2376
      Closes NEXT-2374
      3efc842a
    • JJ Kasper's avatar
      Replace image optimizer IPC call with request handler (#61471) · 179d14e4
      JJ Kasper authored
      This ensures we don't do un-necessary IPC calls during image optimizing
      and leverage the internal request handler for fetching internal static
      images/resources.
      
      x-ref: https://github.com/vercel/next.js/issues/58248
      
      Closes NEXT-2318
      179d14e4
    • Steven's avatar
      chore: refactor image optimization to separate external/internal urls (#61172) · f8fe70dd
      Steven authored
      This PR is refactoring only and doesn't change the logic, therefore no
      tests were added.
      
      The improvement here is readability:
      - separate functions to fetch external vs internal image (in the future,
      we could improve perf of internal image routing)
      - types of the arguments are more specific (rather than passing in
      complete NextConfig)
      
      > Note: Its easier to review with whitespace hidden:
      https://github.com/vercel/next.js/pull/61172/files?w=1
      f8fe70dd
    • Steven's avatar
      fix(image): warn when animated image is missing `unoptimized` prop (#61045) · 5a5f178f
      Steven authored
      This PR adds a warning when attempting to optimize an animated image.
      
      ```jsx
      <Image src="/image.gif" />
      ```
      
      The warning looks like the following:
      
      ```
      The requested resource "/image.gif" is an animated image so it will not be optimized. Consider adding the "unoptimized" property to the <Image>.
      ```
      
      To remove the warning, add the `unoptimized` prop.
      
      ```jsx
      <Image src="/image.gif" unoptimized />
      ```
      
      We don't attempt to optimized animated images because it can be very
      slow (30+ seconds) and sometimes deoptimizeds (the output is larger than
      the input) so its best to serve the animated image as-is.
      
      Closes NEXT-2199
      5a5f178f
    • Balázs Orbán's avatar
      fix(build-output): show stack during CSR bailout warning (#62594) · b0474c81
      Balázs Orbán authored
      We should treat the warning for CSR bailout the same as the hard-error,
      and show the stack trace for either case.
      
      It might be useful to track down the source of this warning.
      
      We have the stack info already, but we only showed it during the error
      logging. We should show it for warnings too.
      
      This is similar to #61200
      
      Closes NEXT-2624
      b0474c81
    • Jiachi Liu's avatar
      Fix extra swc optimizer applied to node_modules in browser layer (#62051) · 1bee5b6c
      Jiachi Liu authored
      Disable swc transform optimizer for node_modules in browser layer of app
      router bundles
      
      Fixes #61858
      Fixes #60644
      Fixes #60920
      Fixes #61740
      Closes NEXT-2418
      
      In browser there could be not only one runtime, it could have both js
      worker and browser. In js worker the `typeof window` is not as same as
      in browser, so disabling the swc optimizer which will replace the code.
      Leave the condition as it as.
      1bee5b6c
    • Donny/강동윤's avatar
      fix(next-swc): Detect `exports.foo` from `cjs_finder` (#61795) · b747e08b
      Donny/강동윤 authored
      ### What?
      
      Patch cjs finder to detect `exports.foo` (instead of module.exports)
      
      ### Why?
      
      This causes an issue for `react.production.min.js`.
      
      x-ref: https://vercel.slack.com/archives/C02HY34AKME/p1707343215879979
      
      ### How?
      
      Closes PACK-2443
      b747e08b
    • Jiachi Liu's avatar
      Fix attempted import error for react (#61791) · af47328c
      Jiachi Liu authored
      ### What
      
      Exclude precompiled react packages from browser layer loaders coverage.
      
      ### Why
      
      Since we're transpiling all the browser layer code now after #59569,
      then SWC will also compile react. But when it compiles
      `react.production.min.js` it gives me with the code and ESM helper
      inserted
      
      ```js
      import { _ as _type_of } from "@swc/helpers/_/_type_of"; // This is not correct
      var l = Symbol.for("react.element"), n = Symbol.for("react.portal"), p = Symbol.for("react.fragment"), q = Sym
      bol.for("react.strict_mode"), r = Symbol.for("react.profiler"), t = Symbol.for("react.provider"), u = Symbol.f
      ```
      
      This makes bundler think it's a ESM package but actually it's CJS, which
      converts the module into `{ default: .., __esModule }` instead of the
      original react module.
      
      When you're using `React.useEffect` or other API through namespace
      import (`import * as React from 'react'`), this will break the module
      exports check in bundling as the property doesn't directly attached to
      the module now. This PR disabled the transform for precompiled react
      packages now and will see the deeper issue in next-swc side later.
      
      Fixes #60890
      Fixes #61185
      
      Closes NEXT-2362
      af47328c
    • Jiachi Liu's avatar
      181e7d52
    • JJ Kasper's avatar
    • JJ Kasper's avatar
      update chunking tests · c1edac79
      JJ Kasper authored
      c1edac79
    • Zack Tanner's avatar
      fix router crash on revalidate + popstate (#62383) · 6aee03de
      Zack Tanner authored
      When the popstate action is fired (as is the case in a browser back
      button), if the page you're going back to has missing cache node data,
      the router will crash.
      
      Almost all router actions will suspend at the app-router level with the
      exception of `ACTION_RESTORE`. This was to address an issue where
      suspending in the router would add enough delay for browser scroll
      restoration behavior not to work.
      
      As a result, when going back to the page with missing data, app-router
      wouldn't suspend but layout-router would suspend on the missing data
      while triggering a lazy fetch. We trigger a server-patch with the
      applied lazy data, but when React replays the render, it will replay the
      branch without the cache node data applied. This results in the router
      getting caught in a loop of suspending, applying the cache node,
      replaying the branch without the cache node, and eventually crashing due
      to an error thrown by React to prevent re-suspending indefinitely.
      
      This adds a property to the cache node to signal if the lazy data has
      been resolved. If it has been, we won't call the server patch action
      again.
      
      Fixes #61336
      Closes NEXT-2438
      6aee03de
    • Zack Tanner's avatar
      fix loading issue when navigating to page with async metadata (#61687) · 8fd3d7ee
      Zack Tanner authored
      ### What
      Client-side transitioning to a page that triggered a loading boundary
      with async metadata would cause the transition to stall, potentially
      getting stuck in a refetch loop.
      
      ### Why
      In layout-router, we trigger a "lazy fetch" when we encounter a segment
      that we don't have cache nodes for. This calls out to the server and
      suspends until the data fetch is resolved, and applied to the router
      tree. However after suspending but before updating the client router, we
      set `childNode.lazyData` to null. When we unsuspend from the server
      patch action, `childNode.rsc` might still be missing and clearing
      `lazyData` means we've blown away the reference to the fetch we already
      had pending, triggering a refetch loop.
      
      ### How
      This removes the logic that mutates the cache node in render, as this is
      not concurrent safe, and doesn't appear to be needed for anything.
      
      Fixes #61117
      Closes NEXT-2361
      
      ---------
      Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
      8fd3d7ee
    • Zack Tanner's avatar
      revert changes to process default routes at build (#61241) · 05b972b8
      Zack Tanner authored
      Reverts changes from #61173 & #60240 (while leaving the tests that were
      added).
      
      There are too many spots where considering `/default` routes as pages
      needs to be carefully considered in different runtimes, and it turns out
      that it's not actually needed to handle the case that it was originally
      added for. I confirmed that the test that added the case it was intended
      to fix (`parallel-routes-catchall-default`, along with the unit tests in
      `normalize-catchall-routes`) are still passing as expected.
      05b972b8
    • Willi#m ⬣'s avatar
      fix parallel route top-level catch-all normalization logic to support nested… · 7026eb21
      Willi#m ⬣ authored
      fix parallel route top-level catch-all normalization logic to support nested explicit (non-catchall) slot routes (#60776)
      
      Fix NEXT-2165
      
      ### What?
      
      Addresses the limitation of #60240, where a dummy `default` file is
      required in parallel route child slot to prevent errors in dev server
      rendering (`TypeError: Cannot read properties of undefined (reading
      'clientModules')`) as well as errors in build and deploy (`Error:
      ENOENT: no such file or directory, lstat
      ‘/vercel/path0/.next/server/app/parallel-route/[section]/@part/[partSlug]/page_client-reference-manifest.js’`)
      
      Without the `default.tsx`, builds and deployments will fail with:
      
      <img width="956" alt="CleanShot 2024-01-18 at 02 12 36@2x"
      src="https://github.com/vercel/next.js/assets/179761/80ba61bd-6ec0-4b16-a393-dc9375227e19">
      
      local dev server will also crash with:
      
      <img width="986" alt="CleanShot 2024-01-18 at 02 13 19@2x"
      src="https://github.com/vercel/next.js/assets/179761/cc500a32-b2f8-47b4-999e-e57cf5141b2f">
      
      > TypeError: Cannot read properties of undefined (reading
      'clientModules')
      
      
      ### Why?
      
      Since `default.tsx` is not a compulsory when you have slot that are
      specific and ends with a dynamic route segment, this PR extends support
      so that it is possible mixing catch-all routes with specific
      non-catchall routes without requiring an additional `default.tsx` .
      
      This PR will allow the following test cases to pass:
      
      ```
      it('should not add the catch-all route to segments that have a more specific [dynamicRoute]', () => {
          const appPaths = {
            '/': ['/page'],
            '/[[...catchAll]]': ['/[[...catchAll]]/page'],
            '/nested/[foo]/[bar]/default': [
              '/nested/[foo]/[bar]/default',
              '/nested/[foo]/[bar]/@slot0/default',
              '/nested/[foo]/[bar]/@slot2/default',
            ],
            '/nested/[foo]/[bar]': [
              '/nested/[foo]/[bar]/@slot0/page',
              '/nested/[foo]/[bar]/@slot1/page',
            ],
            '/nested/[foo]/[bar]/[baz]': [
              '/nested/[foo]/[bar]/@slot0/[baz]/page',
              '/nested/[foo]/[bar]/@slot1/[baz]/page',
            ],
            '/[locale]/nested/[foo]/[bar]/[baz]/[qux]': [
              '/[locale]/nested/[foo]/[bar]/@slot1/[baz]/[qux]/page',
            ],
          }
      
          const initialAppPaths = JSON.parse(JSON.stringify(appPaths))
          normalizeCatchAllRoutes(appPaths)
          expect(appPaths).toMatchObject(initialAppPaths)
        })
      ...
      ```
      
      ```it('should not add the catch-all route to segments that have a more specific [dynamicRoute]', () => {
          const appPaths = {
            '/': ['/page'],
            '/[[...catchAll]]': ['/[[...catchAll]]/page'],
            '/nested/[foo]/[bar]/default': [
              '/nested/[foo]/[bar]/default',
              '/nested/[foo]/[bar]/@slot0/default',
              '/nested/[foo]/[bar]/@slot2/default',
            ],
            '/nested/[foo]/[bar]': [
              '/nested/[foo]/[bar]/@slot0/page',
              '/nested/[foo]/[bar]/@slot1/page',
            ],
            '/nested/[foo]/[bar]/[baz]': [
              '/nested/[foo]/[bar]/@slot0/[baz]/page',
              '/nested/[foo]/[bar]/@slot1/[baz]/page',
            ],
            '/[locale]/nested/[foo]/[bar]/[baz]/[qux]': [
              '/[locale]/nested/[foo]/[bar]/@slot1/[baz]/[qux]/page',
            ],
          }
      ...
      ```
      
      and allow parallel routes defined in this [code
      repro](https://github.com/williamli/nextjs-NEXT-2165) to build.
      
      
      ![image](https://github.com/vercel/next.js/assets/179761/030f4fe1-3a27-41e5-bbd9-bc511f95e5d7)
      
      
      ### How?
      
      `packages/next/src/build/normalize-catchall-routes.ts` is extended to
      check `appPath` to see if it is:
      1. the route is not a catchall
      2. `isMoreSpecific` than the closest `catchAllRoute`.
      
      
      where `isMoreSpecific` is defined as:
      
      ```
      
      function isMoreSpecific(pathname: string, catchAllRoute: string): boolean {
        const pathnameDepth = pathname.split('/').length
        const catchAllRouteDepth = catchAllRoute.split('/').length - 1
        return pathnameDepth > catchAllRouteDepth
      }
      
      ```
      
      ---------
      Co-authored-by: 's avatarZack Tanner <zacktanner@gmail.com>
      7026eb21