1. 25 Sep, 2024 27 commits
    • vercel-release-bot's avatar
      v15.0.0-canary.171 · c13f5023
      vercel-release-bot authored
      c13f5023
    • Josh Story's avatar
      [Breaking] Update Dynamic APIs to be async (#68812) · 05f159df
      Josh Story authored
      Next.js has a number of dynamic APIs that aren't available during
      prerendering. What happens when you access these APIs might differ
      depending on the mode you are using, for instance if you have PPR turned
      on or the newly introduced `dynamicIO` experimental mode. But regardless
      of the mode the underlying API represents accessing something that might
      only be available at render time (dynamic rendering) rather than
      prerender time (build and revalidate rendering)
      
      Unfortunately our current dynamic APIs make certain kinds of modeling
      tricky because they are all synchronous. For instance if we wanted to
      add a feature to Next.js where we started a dynamic render before a
      Request even hits the server it would be interesting to be able to start
      working on everything that does not rely on any dynamic data and then
      once a real Request arrives we can continue the render and provide the
      associated Request context through our dynamic APIs.
      
      If our dynamic APIs were all async we could build something like this
      because they represnt a value that will eventually resolve to some
      Request value. This PR updates most existing dynamic APIs to be async
      rather than sync. This is a breaking change and will need to be paired
      with codemods to realistically adopt. Additionally since this change is
      so invasive I have implemented it in a way to maximize backward
      compatibility by still allowing most synchronous access. The combination
      of codemods, typescript updates, and backward compat functionality
      should make it possible for projects to upgrade to the latest version
      with minimal effort and then follow up with a complete conversion over
      time.
      
      #### `cookies()`
      `cookies()` now returns `Promise<ReadonlyRequestCookies>`. Synchronous
      access to the underlying RequestCookies object is still supported to
      facilitate migration.
      ```tsx
      // ------------ preferred usage
      
      // async Server Component
      const token = (await cookies()).get('token')
      
      // sync Server Component
      import { use } from 'react'
      //...
      const token = use(cookies()).get('token')
      
      // ------------ temporarily allowed usage
      
      // javascript, dev warning at runtime
      const token = cookies().get('token')
      
      // typescript, dev warning at runtime
      import { type UnsafeUnwrappedCookies } from 'next/headers'
      // ...
      const token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token')
      ```
      
      #### `headers()`
      `headers()` now returns `Promise<ReadonlyHeaders>`. Synchronous access
      to the underlying Headers object is still supported to facilitate
      migration.
      ```tsx
      // ------------ preferred usage
      
      // async Server Component
      const header = (await headers()).get('x-foo')
      
      // sync Server Component
      import { use } from 'react'
      //...
      const header = use(headers()).get('x-foo')
      
      // ------------ temporarily allowed usage
      
      // javascript, dev warning at runtime
      const header = headers().get('x-foo')
      
      // typescript, dev warning at runtime
      import { type UnsafeUnwrappedHeaders } from 'next/headers'
      // ...
      const header = (headers() as unknown as UnsafeUnwrappedHeaders).get('x-foo')
      ```
      
      
      #### `draftMode()`
      `draftMode()` now returns `Promise<DraftMode>`. Synchronous access to
      the underlying DraftMode object is still supported to facilitate
      migration.
      ```tsx
      // ------------ preferred usage
      
      // async Server Component
      if ((await draftMode()).isEnabled) { ... }
      
      // sync Server Component
      import { use } from 'react'
      //...
      if (use(draftMode()).isEnabled) { ... }
      
      // ------------ temporarily allowed usage
      
      // javascript, dev warning at runtime
      if (draftMode().isEnabled) { ... }
      
      // typescript, dev warning at runtime
      import { type UnsafeUnwrappedDraftMode} from 'next/headers'
      // ...
      if ((draftMode() as unknown as UnsafeUnwrappedDraftMode).isEnabled) { ... }
      ```
      
      #### `searchParams`
      `searchParams` is now a `Promise<{...}>`. Synchronous access to the
      underlying search params is still supported to facilitate migration.
      ```tsx
      // ------------ preferred usage
      
      // async Page Component
      export default async function Page({
        searchParams
      }: {
        searchParams: Promise<{ foo: string }>
      }) {
        const fooSearchParam = (await searchParams).foo
      }
      
      // sync Page Component
      import { use } from 'react'
      export default function Page({
        searchParams
      }: {
        searchParams: Promise<{ foo: string }>
      }) {
        const fooSearchParam = use(searchParams).foo
      }
      
      // ------------ temporarily allowed usage
      
      // javascript, dev warning at runtime
      export default async function Page({ searchParams}) {
        const fooSearchParam = searchParams.foo
      }
      
      // typescript, dev warning at runtime
      import { type UnsafeUnwrappedSearchParams } from 'next/server'
      export default async function Page({
        searchParams
      }: {
        searchParams: Promise<{ foo: string }>
      }) {
        const syncSearchParams = (searchParams as unknown as UnsafeUnwrappedSearchParams<typeof searchParams>)
        const fooSearchParam = syncSearchParams.foo
      }
      ```
      
      
      
      #### `params`
      `params` is now a `Promise<{...}>`. Synchronous access to the underlying
      params is still supported to facilitate migration. It should be noted
      that while params are not usually dynamic there are certain modes where
      they can be such as fallback prerenders for PPR.
      ```tsx
      // ------------ preferred usage
      
      // async Segment Component
      export default async function Layout({
        params
      }: {
        params: Promise<{ foo: string }>
      }) {
        const fooParam = (await params).foo
      }
      
      // sync Segment Component
      import { use } from 'react'
      export default function Layout({
        params
      }: {
        params: Promise<{ foo: string }>
      }) {
        const fooParam = use(params).foo
      }
      
      // ------------ temporarily allowed usage
      
      // javascript, dev warning at runtime
      export default async function Layout({ params}) {
        const fooParam = params.foo
      }
      
      // typescript, dev warning at runtime
      import { type UnsafeUnwrappedParams } from 'next/headers'
      export default async function Layout({
        params
      }: {
        params: Promise<{ foo: string }>
      }) {
        const syncParams = (params as unknown as UnsafeUnwrappedParams<typeof params>)
        const fooSearchParam = syncParams.foo
      }
      ```
      
      ### Typescript Changes
      When using typescript with Next.js currently it is up to you to author
      types for Pages, Layouts and other Segment components that recieve props
      like `params` and `searchParams`.
      
      Next comes with some build-time type checking to ensure you have not
      improperly typed various top level module exports however the current
      type assertions for `params` and `searchParams` is `any`. This isn't
      very helpful because it allows you to erroneously type these props.
      
      `searchParams` is tricky because while the default type is a dictionary
      object parsed using node.js url parsing it is possible to customize when
      running a custom Next.js server. However we can ensure that you
      correctly type the prop as a Promise so with this change the validated
      type for `searchParams` will be `Promise<any>`.
      
      In the long run we will look at updating the `searchParams` underlying
      type to be URLSearchParams so we can move away from supporting
      customized parsing during rendering and we can get even more explicit
      about valid types.
      
      `params` is more straight forward because the framework controls the
      actual `params` prop implementation and no customization is possible. In
      the long run we want to enforce you are only typing params that are
      valid for the Layout level your file is located in but for now we are
      updating the allowed type to be `Promise<{[key: string]: string |
      string[] | undefined }>`.
      
      These new type restrictions may also require fixes before being able to
      successfully build a project that updates to include these breaking
      changes. These changes will also not always be codemodable because it is
      valid to type the entire component using an opaque type like `Props`
      which our codemods may not have an ability to introspect or modify.
      05f159df
    • vercel-release-bot's avatar
      v15.0.0-canary.170 · e92ea383
      vercel-release-bot authored
      e92ea383
    • PapatMayuri's avatar
      Updated the example of with-draft-js to utilize the App Router. (#70045) · 9fcecdad
      PapatMayuri authored
      This PR updates the with-draft-js example for using the App Router. 
      Here are the changes that have been made:
      
      - Renamed the "pages" folder to the "app" folder.
      - Added the layout.tsx file as part of the App Router.
      - Updated the package.json file.
      
      CC: @samcx
      
      ---------
      Co-authored-by: 's avatarSam Ko <sam@vercel.com>
      9fcecdad
    • vercel-release-bot's avatar
      v15.0.0-canary.169 · a4a1fe77
      vercel-release-bot authored
      a4a1fe77
    • JJ Kasper's avatar
      Ensure validator is included in vendored AMP validator (#70482) · 98e5ee74
      JJ Kasper authored
      This aims to provide more stability so we aren't hitting the network to
      grab the validator more than we need to and instead leverage the one
      from build instead which should also reduce some test flakiness we've
      been seeing.
      98e5ee74
    • JJ Kasper's avatar
    • Wyatt Johnson's avatar
      fix: updated typescript types for React API's (#70410) · e2ae5e9d
      Wyatt Johnson authored
      <!-- Thanks for opening a PR! Your contribution is much appreciated.
      To make sure your PR is handled as smoothly as possible we request that
      you follow the checklist sections below.
      Choose the right checklist for the change(s) that you're making:
      
      ## For Contributors
      
      ### Improving Documentation
      
      - Run `pnpm prettier-fix` to fix formatting issues before opening the
      PR.
      - Read the Docs Contribution Guide to ensure your contribution follows
      the docs guidelines:
      https://nextjs.org/docs/community/contribution-guide
      
      ### Adding or Updating Examples
      
      - The "examples guidelines" are followed from our contributing doc
      https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
      - Make sure the linting passes by running `pnpm build && pnpm lint`. See
      https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md
      
      ### Fixing a bug
      
      - Related issues linked using `fixes #number`
      - Tests added. See:
      https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
      - Errors have a helpful link attached, see
      https://github.com/vercel/next.js/blob/canary/contributing.md
      
      ### Adding a feature
      
      - Implements an existing feature request or RFC. Make sure the feature
      request has been accepted for implementation before opening a PR. (A
      discussion must be opened, see
      https://github.com/vercel/next.js/discussions/new?category=ideas)
      - Related issues/discussions are linked using `fixes #number`
      - e2e tests added
      (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
      - Documentation added
      - Telemetry added. In case of a feature if it's used or not.
      - Errors have a helpful link attached, see
      https://github.com/vercel/next.js/blob/canary/contributing.md
      
      
      ## For Maintainers
      
      - Minimal description (aim for explaining to someone not on the team to
      understand the PR)
      - When linking to a Slack thread, you might want to share details of the
      conclusion
      - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
      - Add review comments if necessary to explain to the reviewer the logic
      behind a change
      
      ### What?
      
      ### Why?
      
      ### How?
      
      Closes NEXT-
      Fixes #
      
      -->
      
      
      This adds and updates some Typescript types for the React libraries used
      by Next.js. As a result some of the code was modified to match.
      e2ae5e9d
    • tranvanhieu01012002's avatar
      fix(example): Change hostname in docker multiple env examples (#70105) · 3d6ecf38
      tranvanhieu01012002 authored
      <!-- Thanks for opening a PR! Your contribution is much appreciated.
      To make sure your PR is handled as smoothly as possible we request that
      you follow the checklist sections below.
      Choose the right checklist for the change(s) that you're making:
      
      ## For Contributors
      
      ### Improving Documentation
      
      - Run `pnpm prettier-fix` to fix formatting issues before opening the
      PR.
      - Read the Docs Contribution Guide to ensure your contribution follows
      the docs guidelines:
      https://nextjs.org/docs/community/contribution-guide
      
      ### Adding or Updating Examples
      
      - The "examples guidelines" are followed from our contributing doc
      https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
      - Make sure the linting passes by running `pnpm build && pnpm lint`. See
      https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md
      
      ### Fixing a bug
      
      - Related issues linked using `fixes #number`
      - Tests added. See:
      https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
      - Errors have a helpful link attached, see
      https://github.com/vercel/next.js/blob/canary/contributing.md
      
      ### Adding a feature
      
      - Implements an existing feature request or RFC. Make sure the feature
      request has been accepted for implementation before opening a PR. (A
      discussion must be opened, see
      https://github.com/vercel/next.js/discussions/new?category=ideas)
      - Related issues/discussions are linked using `fixes #number`
      - e2e tests added
      (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
      - Documentation added
      - Telemetry added. In case of a feature if it's used or not.
      - Errors have a helpful link attached, see
      https://github.com/vercel/next.js/blob/canary/contributing.md
      
      
      ## For Maintainers
      
      - Minimal description (aim for explaining to someone not on the team to
      understand the PR)
      - When linking to a Slack thread, you might want to share details of the
      conclusion
      - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
      - Add review comments if necessary to explain to the reviewer the logic
      behind a change
      -->
      ### What?
      
      Update the docker with multiple env examples by the correct hostname.
      
      ### Why?
      I am verry happy when I see this example, I need to deploy a project
      with multiple environments,
      I tested current example for my machine, but it started but it didn't
      connect to app.
      
      I really try this example
      https://github.com/vercel/next.js/tree/canary/examples/with-docker-multi-env.
      but it doesn't connect to nextjs container because the hostname
      configuation is incorrect.
      
      ### How?
      I created this PR to change hostname from host ```localhost``` to
      ```0.0.0.0``` in docker with multiple environments
      Closes NEXT-
      No fixing any issues
      Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
      3d6ecf38
    • Sebastian Markbåge's avatar
    • Tobias Koppers's avatar
      [Turbopack] fix a bunch of bugs and inefficiencies in the call graph (#70243) · d5b252aa
      Tobias Koppers authored
      ### What?
      
      * switch remove vs change order
      * take_collectibles does not need to return an Option
      * avoid double Option
      * fix edges set bugs
      * remove collectibles from outdated collectibles
      * remove unneccessary remove
      d5b252aa
    • Donny/강동윤's avatar
    • Jordy Fontoura's avatar
      Fix comment numbering in middleware example in docs (#70465) · 1d62f471
      Jordy Fontoura authored
      ### What?
      This PR fixes a numbering issue in the example code of the
      documentation. The comments in the code jumped from step 3 to step 5, so
      I renumbered them to maintain the correct sequence.
      
      ### Why?
      Correcting the comment numbering improves the clarity and readability of
      the code, ensuring that readers can follow the steps in the right order.
      
      ### How?
      I changed step 5 to step 4 and step 6 to step 5, adjusting the comment
      sequence without altering the logic or functionality of the code.
      
      ### Evidence
      
      
      ![image](https://github.com/user-attachments/assets/12b15de3-2cc3-4dca-9185-cde5a97cd0c3)
      Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
      1d62f471
    • Benjamin Woodruff's avatar
      codemod(turbopack): Inline trivial uses of `let this = self` (#70431) · 94c4e5da
      Benjamin Woodruff authored
      A follow-up to the #70431 codemod diff, which leave behind a lot of
      trivial `let this = self;` statements.
      
      Using the following ast-grep rule config:
      
      ```yaml
      language: rust
      id: inline_trivial_this
      
      utils:
        block-with-this:
          all:
          - pattern: $BLOCK
          - kind: block
          - has:
              all:
                - pattern: let this = $SELF;
                - any:
                  - pattern: let this = self;
                  - pattern: let this = &self.0;
      
      rule:
        matches: block-with-this
      
      rewriters:
        - id: remove-let-this
          rule:
            pattern: let this = $SELF;
          fix: ""
        - id: inline-this-auto-deref
          rule:
            pattern: this
            inside:
              kind: field_expression
          transform:
            SELF_AUTO_DEREF:
              replace:
                source: $SELF
                replace: "&?(?<INNER>.*)"
                by: "$INNER"
          fix: $SELF_AUTO_DEREF
        - id: inline-this
          rule:
            pattern: this
          fix: $SELF
      
      transform:
        NEW_BLOCK:
          rewrite:
            source: $BLOCK
            rewriters:
              - remove-let-this
              - inline-this-auto-deref
              - inline-this
      
      fix:
        $NEW_BLOCK
      ```
      
      Applied with
      
      ```
      sg scan -U -r ../codemod_inline_trivial_this.yml .
      ```
      94c4e5da
    • Sebastian "Sebbie" Silbermann's avatar
    • vercel-release-bot's avatar
      v15.0.0-canary.168 · 090dc453
      vercel-release-bot authored
      090dc453
    • Niklas Mischkulnig's avatar
      Turbopack: fix another case of edge runtime warnings (#70455) · 7e32b38b
      Niklas Mischkulnig authored
      Closes PACK-3276
      7e32b38b
    • Alexander Lyon's avatar
      add @taskr/esnext to pnpm root to resolve hoisting issues (#70349) · 178f8a00
      Alexander Lyon authored
      ### What?
      
      Add `@taskr/esnext` to the root package.json to get around hoisting
      rules breaking `taskr`'s automatic discovery (and fix builds on some
      platforms where it is broken).
      
      ### Why?
      
      Some types of 'magically' imported packages (such as taskr automatically
      supporting es6 by just adding a package, or drizzle automatically
      finding a database driver) aren't hoisted on some platforms in the way
      the tool expects, breaking it. The symptom of this is taskr throwing a
      syntax error on some platforms, because we use ES6 syntax and it can't
      find the package it needs to support it.
      
      ### How?
      
      By adding it to the root package.json we ensure it is always hoisted in
      the way taskr expects.
      178f8a00
    • Jam Balaya's avatar
      f7fca5da
    • Jiachi Liu's avatar
      test: disable next/test tests (#70460) · b9a2e48b
      Jiachi Liu authored
      b9a2e48b
    • Niklas Mischkulnig's avatar
      Add cause to turbopack-node error (#70456) · 6c47eaa2
      Niklas Mischkulnig authored
      Occurs for example in
      ```
      Failed to load chunk server/chunks/08b5e__pnpm_560cbe._.js
      	....
      Caused by SyntaxError: Identifier 'require' has already been declared
      	....
      ```
      6c47eaa2
    • Niklas Mischkulnig's avatar
      Turbopack: allow shadowing the `require` global in ESM (#70453) · 039f5809
      Niklas Mischkulnig authored
      Closes PACK-3275
      
      Among other things, a regression from
      https://github.com/vercel/next.js/pull/70255, but it could also happen
      with `__dirname`.
      
      To prevent `SyntaxError: Identifier 'require' has already been declared`
      039f5809
    • Tobias Koppers's avatar
      [Turbopack] fix rsc chunking optimization (#70461) · 584f328a
      Tobias Koppers authored
      ### What?
      
      * While looking for server component entries and client components, look
      for server utils too
      * Create a separate chunk group for server utils
      * Mark all user imports in the rsc entrypoint has server component
      entries
      * Fix order of imports to allow proper caching
      
      ---------
      Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
      584f328a
    • vercel-release-bot's avatar
      v15.0.0-canary.167 · 5d07511c
      vercel-release-bot authored
      5d07511c
    • Benjamin Woodruff's avatar
      codemod(turbopack): Rewrite `self: Vc<Self>` as `&self` in trivial cases (#70412) · ea1618d3
      Benjamin Woodruff authored
      Noticed this pattern while touching `turbo-tasks-fs`.
      
      Functions that take `self: Vc<Self>` and immediately await it without ever using the `Vc` version of the argument should just take `&self` as an argument.
      
      There are some remaining cleanup opportunities here, which I have some still-WIP codemods for:
      
      - [x] If we have a trivial `let this = self` or `let this = &self.0`, we should rewrite references to `this` to use `self`. *Done in #70431*
      - [ ] If we can be sure that a function never returns anything other than `Ok(...)`, we should remove `Result` from the return type.
      
      Those changes need to be performed as a separate codemod pass due to limitations with `ast-grep`.
      
      ## How?
      
      ast-grep: https://ast-grep.github.io/
      
      Using the ast-grep config: https://gist.github.com/bgw/b7bc0a921cf3e3447acaf8feda60b518
      
      Ran it with:
      
      ```
      sg scan -U -r ../codemod_rewrite_vc_self.yml .
      ```
      
      ## Should this be a lint?
      
      **Maybe.** I've managed to get the false-positive rate down to zero on our existing codebase (at the cost of missing some opportunities for cleanup).
      
      ast-grep supports running as a lint rule, and this can be autofixed. However, if we fix the `let this = ...` and `Result<...>` cases as well with additional lint rules, **this might be a bit annoying as it'll trigger cascading lint rules**. You *might* need to run the linter multiple times for it to eventually settle.
      
      It does not appear to be possible to do all these changes in a single lint rule, as we require modifying overlapping ranges of code (which ast-grep sadly doesn't seem to support with the `rewriters` rules).
      
      We should also consider `dynlint` before heavily investing into `ast-grep` as a linter: https://github.com/trailofbits/dylint
      ea1618d3
    • Donny/강동윤's avatar
      refactor(turbopack): Cleanup code for tree shaking (#70385) · dfd5bb33
      Donny/강동윤 authored
      ### What?
      
      Minor refactorings extracted from other PRs.
      
      ### Why?
      
      To make WIP small
      dfd5bb33
    • JJ Kasper's avatar
  2. 24 Sep, 2024 13 commits