Skip to content

Commit

Permalink
Merge branch 'canary' into kdy1/swc-core-0-93
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra authored Jun 11, 2024
2 parents d47e677 + fbcc21a commit d1a8c76
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default function Layout({ children }) {

The use case for `unstable_after()` is to process secondary tasks without blocking the primary response. It's similar to using the platform's [`waitUntil()`](https://vercel.com/docs/functions/functions-api-reference) or removing `await` from a promise, but with the following differences:

- **[`waitUntil()`]**: accepts promise and enqueues a task to be executed during the lifecycle of the request, whereas `unstable_after()` accepts a callback that will be executed **after** the response is finished.
- **`waitUntil()`**: accepts a promise and enqueues a task to be executed during the lifecycle of the request, whereas `unstable_after()` accepts a callback that will be executed **after** the response is finished.
- **Removing `await`**: starts executing during the response, which uses resources. It's also not reliable in serverless environments as the function stops computation immediately after the response is sent, potentially interrupting the task.

We recommend using `unstable_after()` as it has been designed to consider other Next.js APIs and contexts.
Expand Down
44 changes: 4 additions & 40 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,8 @@
const { quote } = require('shell-quote')
const { ESLint } = require('eslint')

const eslint = new ESLint()

/**
* Escape filenames to ensure that spaces and such aren't interpreted as
* separators.
*
* @param {string[]} filenames
* @returns {string[]}
*/
function escape(filenames) {
if (process.platform === 'win32') {
return filenames
}

return filenames.map((filename) => quote([filename]).replace(/\\@/g, '@'))
}

module.exports = {
'*.{js,jsx,mjs,ts,tsx,mts}': async (filenames) => {
const escapedFileNames = escape(filenames).join(' ')
const eslintFileNames = await Promise.all(
filenames.map(async (filename) => {
const ignored = await eslint.isPathIgnored(filename)
return ignored ? null : filename
})
)

return [
`prettier --with-node-modules --ignore-path .prettierignore --write ${escapedFileNames}`,
`eslint --no-ignore --fix ${eslintFileNames
.filter((filename) => filename !== null)
.map((filename) => {
return `"${filename}"`
})
.join(' ')}`,
`git add ${escapedFileNames}`,
]
},
'*.{js,jsx,mjs,ts,tsx,mts}': [
'prettier --with-node-modules --ignore-path .prettierignore --write',
'eslint --fix',
],
'*.{json,md,mdx,css,html,yml,yaml,scss}': [
'prettier --with-node-modules --ignore-path .prettierignore --write',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the

You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load Inter, a custom Google Font.
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load Inter, a custom Google Font.
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the

You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load Inter, a custom Google Font.
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load Inter, a custom Google Font.
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You can start editing the page by modifying `pages/index.js`. The page auto-upda

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/pages/building-your-application/routing/api-routes) instead of React pages.

This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load Inter, a custom Google Font.
This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You can start editing the page by modifying `pages/index.tsx`. The page auto-upd

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/pages/building-your-application/routing/api-routes) instead of React pages.

This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load Inter, a custom Google Font.
This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You can start editing the page by modifying `pages/index.js`. The page auto-upda

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/pages/building-your-application/routing/api-routes) instead of React pages.

This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load Inter, a custom Google Font.
This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You can start editing the page by modifying `pages/index.tsx`. The page auto-upd

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/pages/building-your-application/routing/api-routes) instead of React pages.

This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load Inter, a custom Google Font.
This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

Expand Down
26 changes: 21 additions & 5 deletions packages/font/src/google/font-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,17 @@
"subsets": ["devanagari", "latin", "latin-ext"]
},
"Anaheim": {
"weights": ["400"],
"weights": ["400", "500", "600", "700", "800", "variable"],
"styles": ["normal"],
"subsets": ["latin", "latin-ext"]
"axes": [
{
"tag": "wght",
"min": 400,
"max": 800,
"defaultValue": 400
}
],
"subsets": ["latin", "latin-ext", "vietnamese"]
},
"Andada Pro": {
"weights": ["400", "500", "600", "700", "800", "variable"],
Expand Down Expand Up @@ -4771,7 +4779,7 @@
"Holtwood One SC": {
"weights": ["400"],
"styles": ["normal"],
"subsets": ["latin"]
"subsets": ["latin", "latin-ext"]
},
"Homemade Apple": {
"weights": ["400"],
Expand Down Expand Up @@ -7096,8 +7104,16 @@
"subsets": ["latin", "latin-ext"]
},
"Monda": {
"weights": ["400", "700"],
"weights": ["400", "500", "600", "700", "variable"],
"styles": ["normal"],
"axes": [
{
"tag": "wght",
"min": 400,
"max": 700,
"defaultValue": 400
}
],
"subsets": ["latin", "latin-ext", "vietnamese"]
},
"Monofett": {
Expand Down Expand Up @@ -8292,7 +8308,7 @@
"defaultValue": 400
}
],
"subsets": ["hebrew", "latin", "latin-ext"]
"subsets": ["cyrillic-ext", "greek-ext", "hebrew", "latin", "latin-ext"]
},
"Noto Sans Imperial Aramaic": {
"weights": ["400"],
Expand Down
29 changes: 22 additions & 7 deletions packages/font/src/google/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,15 +926,22 @@ export declare function Amita<
}): T extends undefined ? NextFont : NextFontWithVariable
export declare function Anaheim<
T extends CssVariable | undefined = undefined,
>(options: {
weight: '400' | Array<'400'>
>(options?: {
weight?:
| '400'
| '500'
| '600'
| '700'
| '800'
| 'variable'
| Array<'400' | '500' | '600' | '700' | '800'>
style?: 'normal' | Array<'normal'>
display?: Display
variable?: T
preload?: boolean
fallback?: string[]
adjustFontFallback?: boolean
subsets?: Array<'latin' | 'latin-ext'>
subsets?: Array<'latin' | 'latin-ext' | 'vietnamese'>
}): T extends undefined ? NextFont : NextFontWithVariable
export declare function Andada_Pro<
T extends CssVariable | undefined = undefined,
Expand Down Expand Up @@ -8419,7 +8426,7 @@ export declare function Holtwood_One_SC<
preload?: boolean
fallback?: string[]
adjustFontFallback?: boolean
subsets?: Array<'latin'>
subsets?: Array<'latin' | 'latin-ext'>
}): T extends undefined ? NextFont : NextFontWithVariable
export declare function Homemade_Apple<
T extends CssVariable | undefined = undefined,
Expand Down Expand Up @@ -12815,8 +12822,14 @@ export declare function Molle<
}): T extends undefined ? NextFont : NextFontWithVariable
export declare function Monda<
T extends CssVariable | undefined = undefined,
>(options: {
weight: '400' | '700' | Array<'400' | '700'>
>(options?: {
weight?:
| '400'
| '500'
| '600'
| '700'
| 'variable'
| Array<'400' | '500' | '600' | '700'>
style?: 'normal' | Array<'normal'>
display?: Display
variable?: T
Expand Down Expand Up @@ -14680,7 +14693,9 @@ export declare function Noto_Sans_Hebrew<
preload?: boolean
fallback?: string[]
adjustFontFallback?: boolean
subsets?: Array<'hebrew' | 'latin' | 'latin-ext'>
subsets?: Array<
'cyrillic-ext' | 'greek-ext' | 'hebrew' | 'latin' | 'latin-ext'
>
axes?: 'wdth'[]
}): T extends undefined ? NextFont : NextFontWithVariable
export declare function Noto_Sans_Imperial_Aramaic<
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/build/webpack/loaders/next-app-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ async function createAppRouteCode({
const isDynamicRouteExtension = pageExtensions.includes(ext)

resolvedPagePath = `next-metadata-route-loader?${stringify({
page,
filePath: resolvedPagePath,
isDynamicRouteExtension: isDynamicRouteExtension ? '1' : '0',
})}!?${WEBPACK_RESOURCE_QUERIES.metadataRoute}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const cacheHeader = {
}

type MetadataRouteLoaderOptions = {
page: string
// Using separate argument to avoid json being parsed and hit error
// x-ref: https://github.com/vercel/next.js/pull/62615
filePath: string
isDynamicRouteExtension: '1' | '0'
isDynamicMultiRoute: '1' | '0'
}

export function getFilenameAndExtension(resourcePath: string) {
Expand Down Expand Up @@ -248,6 +248,7 @@ const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction<MetadataRouteLo
async function () {
const { isDynamicRouteExtension, filePath } = this.getOptions()
const { name: fileBaseName } = getFilenameAndExtension(filePath)
this.addDependency(filePath)

let code = ''
if (isDynamicRouteExtension === '1') {
Expand Down
11 changes: 11 additions & 0 deletions packages/next/src/lib/metadata/resolvers/resolve-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ describe('resolveAbsoluteUrlWithPathname', () => {
'https://example.com/foo?bar'
)
})

it('should not add trailing slash to relative url that matches file pattern', () => {
expect(resolver('/foo.html')).toBe('https://example.com/foo.html')
expect(resolver('/foo.html?q=v')).toBe('https://example.com/foo.html?q=v')
expect(resolver(new URL('/.well-known/bar.jpg', metadataBase))).toBe(
'https://example.com/.well-known/bar.jpg/'
)
expect(resolver(new URL('/foo.html', metadataBase))).toBe(
'https://example.com/foo.html'
)
})
})
})

Expand Down
20 changes: 18 additions & 2 deletions packages/next/src/lib/metadata/resolvers/resolve-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ function resolveRelativeUrl(url: string | URL, pathname: string): string | URL {
return url
}

// The regex is matching logic from packages/next/src/lib/load-custom-routes.ts
const FILE_REGEX =
/^(?:\/((?!\.well-known(?:\/.*)?)(?:[^/]+\/)*[^/]+\.\w+))(\/?|$)/i
function isFilePattern(pathname: string): boolean {
return FILE_REGEX.test(pathname)
}

// Resolve `pathname` if `url` is a relative path the compose with `metadataBase`.
function resolveAbsoluteUrlWithPathname(
url: string | URL,
Expand All @@ -110,18 +117,27 @@ function resolveAbsoluteUrlWithPathname(
// - Doesn't have query
if (trailingSlash && !resolvedUrl.endsWith('/')) {
let isRelative = resolvedUrl.startsWith('/')
let isExternal = false
let hasQuery = resolvedUrl.includes('?')
let isExternal = false
let isFileUrl = false

if (!isRelative) {
try {
const parsedUrl = new URL(resolvedUrl)
isExternal =
metadataBase != null && parsedUrl.origin !== metadataBase.origin
isFileUrl = isFilePattern(parsedUrl.pathname)
} catch {
// If it's not a valid URL, treat it as external
isExternal = true
}
if (!isExternal && !hasQuery) return `${resolvedUrl}/`
if (
// Do not apply trailing slash for file like urls, aligning with the behavior with `trailingSlash`
!isFileUrl &&
!isExternal &&
!hasQuery
)
return `${resolvedUrl}/`
}
}

Expand Down
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { nextTestSetup } from 'e2e-utils'
import crypto from 'crypto'

function generateMD5(text: string) {
const hash = crypto.createHash('md5')
hash.update(text)
return hash.digest('hex')
}

describe('app dir - metadata static routes cache', () => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
})

it('should generate different content after replace the static metadata file', async () => {
await next.build()

const faviconBuildContent = await next.readFile(
'.next/server/app/favicon.ico.body'
)
const opengrpahImageBuildContent = await next.readFile(
'.next/server/app/opengraph-image.png.body'
)

const faviconMd5 = generateMD5(faviconBuildContent)
const opengraphImageMd5 = generateMD5(opengrpahImageBuildContent)

// Update favicon and opengraph image
const newFaviconContent = await next.readFile('app/favicon.ico.new')
await next.patchFile('app/favicon.ico', newFaviconContent)

const newOpengraphImageContent = await next.readFile(
'app/opengraph-image.png.new'
)
await next.patchFile('app/opengraph-image.png', newOpengraphImageContent)

await next.build()
const faviconBuildContentNew = await next.readFile(
'.next/server/app/favicon.ico.body'
)
const opengrpahImageBuildContentNew = await next.readFile(
'.next/server/app/opengraph-image.png.body'
)

const faviconMd5New = generateMD5(faviconBuildContentNew)
const opengraphImageMd5New = generateMD5(opengrpahImageBuildContentNew)

expect(faviconMd5).not.toBe(faviconMd5New)
expect(opengraphImageMd5).not.toBe(opengraphImageMd5New)
})
})
Loading

0 comments on commit d1a8c76

Please sign in to comment.