Skip to content

Commit

Permalink
Merge branch 'patch-1' of github.com:gsimone/next.js into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Apr 11, 2022
2 parents 7bbd2cb + 5158c8d commit 52b4214
Show file tree
Hide file tree
Showing 27 changed files with 79 additions and 108 deletions.
54 changes: 1 addition & 53 deletions docs/advanced-features/using-mdx.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,6 @@ The following steps outline how to setup `@next/mdx` in your Next.js project:
})
```

> Using MDX plugins will require using next.config.mjs because all the plugins are [ECMAScript modules](https://nodejs.org/api/esm.html)
```js
// next.config.mjs
import createMDX from '@next/mdx'

const withMDX = createMDX({
extension: /\.mdx?$/,
options: {
remarkPlugins: [],
rehypePlugins: [],
// If you use `MDXProvider`, uncomment the following line.
// providerImportSource: "@mdx-js/react",
},
})
module.exports = withMDX({
// Append the default value with md extensions
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
})
```

3. Create a new MDX page within the `/pages` directory:

```bash
Expand All @@ -85,14 +64,6 @@ The following steps outline how to setup `@next/mdx` in your Next.js project:
- package.json
```

## next-mdx-remote

On a remote database, use next-mdx-remote https://github.com/hashicorp/next-mdx-remote

## @mdx-js/mdx

Handle mdx strings client side, probably @mdx-js/mdx see official docs https://mdxjs.com/docs/getting-started/

## Using Components, Layouts and Custom Elements

You can now import a React component directly inside your MDX page:
Expand All @@ -115,7 +86,7 @@ Checkout my React component:

### Frontmatter

Frontmatter is a YAML like key/value pairing that can be used to store data about a page. `@next/mdx` does **not** support frontmatter by default, though there are many solutions for adding frontmatter to your MDX content, such as [gray-matter](https://github.com/jonschlinkert/gray-matter) using [remark-frontmatter](https://github.com/remarkjs/remark-frontmatter).
Frontmatter is a YAML like key/value pairing that can be used to store data about a page. `@next/mdx` does **not** support frontmatter by default, though there are many solutions for adding frontmatter to your MDX content, such as [gray-matter](https://github.com/jonschlinkert/gray-matter).

To access page metadata with `@next/mdx`, you can export a meta object from within the `.mdx` file:

Expand All @@ -127,26 +98,6 @@ author: 'Rich Haines'
# My MDX page
```

```js
// next.config.mjs
import createMDX from '@next/mdx'
import remarkFrontmatter from 'remark-frontmatter'

const withMDX = createMDX({
extension: /\.mdx?$/,
options: {
remarkPlugins: [remarkFrontmatter],
rehypePlugins: [],
// If you use `MDXProvider`, uncomment the following line.
// providerImportSource: "@mdx-js/react",
},
})
module.exports = withMDX({
// Append the default value with md extensions
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
})
```

### Layouts

To add a layout to your MDX page, create a new component and import it into the MDX page. Then you can wrap the MDX page with your layout component:
Expand Down Expand Up @@ -257,8 +208,5 @@ If you use it across the site you may want to add the provider to `_app.js` so a

- [MDX](https://mdxjs.com)
- [`@next/mdx`](https://www.npmjs.com/package/@next/mdx)
- [next-mdx-remote](https://github.com/hashicorp/next-mdx-remote)
- [next-mdx-remote example](https://github.com/vercel/next.js/tree/canary/examples/with-mdx-remote)
- [remark](https://github.com/remarkjs/remark)
- [rehype](https://github.com/rehypejs/rehype)
- [ECMAScript modules](https://nodejs.org/api/esm.html)
2 changes: 1 addition & 1 deletion docs/api-reference/next.config.js/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ The commented lines are the place where you can put the configs allowed by `next

However, none of the configs are required, and it's not necessary to understand what each config does. Instead, search for the features you need to enable or modify in this section and they will show you what to do.

> Avoid using new JavaScript features not available in your target Node.js version. `next.config.js` will not be parsed by Webpack, the compiler or TypeScript.
> Avoid using new JavaScript features not available in your target Node.js version. `next.config.js` will not be parsed by Webpack, Babel or TypeScript.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "12.1.5-canary.4"
"version": "12.1.5-canary.5"
}
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-next-app",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"keywords": [
"react",
"next",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-config-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-next",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"description": "ESLint configuration used by NextJS.",
"main": "index.js",
"license": "MIT",
Expand All @@ -9,7 +9,7 @@
"directory": "packages/eslint-config-next"
},
"dependencies": {
"@next/eslint-plugin-next": "12.1.5-canary.4",
"@next/eslint-plugin-next": "12.1.5-canary.5",
"@rushstack/eslint-patch": "1.0.8",
"@typescript-eslint/parser": "5.10.1",
"eslint-import-resolver-node": "0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/eslint-plugin-next",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"description": "ESLint plugin for NextJS.",
"main": "lib/index.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-bundle-analyzer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/bundle-analyzer",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-codemod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/codemod",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"license": "MIT",
"dependencies": {
"chalk": "4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-env/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/env",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"keywords": [
"react",
"next",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-mdx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/mdx",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-plugin-storybook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/plugin-storybook",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-storybook"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-module/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-module",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-nomodule/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-nomodule",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/swc",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"private": true,
"scripts": {
"build-native": "napi build --platform --cargo-name next_swc_napi native",
Expand Down
14 changes: 7 additions & 7 deletions packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"description": "The React Framework",
"main": "./dist/server/next.js",
"license": "MIT",
Expand Down Expand Up @@ -69,7 +69,7 @@
]
},
"dependencies": {
"@next/env": "12.1.5-canary.4",
"@next/env": "12.1.5-canary.5",
"caniuse-lite": "^1.0.30001283",
"postcss": "8.4.5",
"styled-jsx": "5.0.1"
Expand Down Expand Up @@ -117,11 +117,11 @@
"@hapi/accept": "5.0.2",
"@napi-rs/cli": "2.4.4",
"@napi-rs/triples": "1.1.0",
"@next/polyfill-module": "12.1.5-canary.4",
"@next/polyfill-nomodule": "12.1.5-canary.4",
"@next/react-dev-overlay": "12.1.5-canary.4",
"@next/react-refresh-utils": "12.1.5-canary.4",
"@next/swc": "12.1.5-canary.4",
"@next/polyfill-module": "12.1.5-canary.5",
"@next/polyfill-nomodule": "12.1.5-canary.5",
"@next/react-dev-overlay": "12.1.5-canary.5",
"@next/react-refresh-utils": "12.1.5-canary.5",
"@next/swc": "12.1.5-canary.5",
"@peculiar/webcrypto": "1.3.1",
"@taskr/clear": "1.1.0",
"@taskr/esnext": "1.1.0",
Expand Down
28 changes: 17 additions & 11 deletions packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ function addPathPrefix(path: string, prefix?: string) {
)
}

function hasPathPrefix(path: string, prefix: string) {
path = pathNoQueryHash(path)
return path === prefix || path.startsWith(prefix + '/')
}

export function getDomainLocale(
path: string,
locale?: string | false,
Expand Down Expand Up @@ -153,16 +158,18 @@ export function addLocale(
defaultLocale?: string
) {
if (process.env.__NEXT_I18N_SUPPORT) {
const pathname = pathNoQueryHash(path)
const pathLower = pathname.toLowerCase()
const localeLower = locale && locale.toLowerCase()
if (locale && locale !== defaultLocale) {
const pathname = pathNoQueryHash(path)
const pathLower = pathname.toLowerCase()
const localeLower = locale.toLowerCase()

return locale &&
locale !== defaultLocale &&
!pathLower.startsWith('/' + localeLower + '/') &&
pathLower !== '/' + localeLower
? addPathPrefix(path, '/' + locale)
: path
if (
!hasPathPrefix(pathLower, '/' + localeLower) &&
!hasPathPrefix(pathLower, '/api')
) {
return addPathPrefix(path, '/' + locale)
}
}
}
return path
}
Expand Down Expand Up @@ -194,8 +201,7 @@ function pathNoQueryHash(path: string) {
}

export function hasBasePath(path: string): boolean {
path = pathNoQueryHash(path)
return path === basePath || path.startsWith(basePath + '/')
return hasPathPrefix(path, basePath)
}

export function addBasePath(path: string): string {
Expand Down
15 changes: 1 addition & 14 deletions packages/next/taskfile-swc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ module.exports = function (task) {
function* (
file,
serverOrClient,
{
stripExtension,
keepImportAssertions = false,
interopClientDefaultExport = false,
} = {}
{ stripExtension, keepImportAssertions = false } = {}
) {
// Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return
Expand Down Expand Up @@ -117,15 +113,6 @@ module.exports = function (task) {
if (output.map) {
const map = `${file.base}.map`

if (interopClientDefaultExport) {
output.code += `
if (typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) {
Object.assign(exports.default, exports);
module.exports = exports.default;
}
`
}

output.code += Buffer.from(`\n//# sourceMappingURL=${map}`)

// add sourcemap to `files` array
Expand Down
2 changes: 1 addition & 1 deletion packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,7 @@ export async function nextbuild(task, opts) {
export async function client(task, opts) {
await task
.source(opts.src || 'client/**/*.+(js|ts|tsx)')
.swc('client', { dev: opts.dev, interopClientDefaultExport: true })
.swc('client', { dev: opts.dev })
.target('dist/client')
notify('Compiled client files')
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dev-overlay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/react-dev-overlay",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"description": "A development-only overlay for developing React applications.",
"repository": {
"url": "vercel/next.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-refresh-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/react-refresh-utils",
"version": "12.1.5-canary.4",
"version": "12.1.5-canary.5",
"description": "An experimental package providing utilities for React Refresh.",
"repository": {
"url": "vercel/next.js",
Expand Down
4 changes: 4 additions & 0 deletions test/integration/i18n-support/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export default function Page(props) {
<a id="to-gssp-slug">to /gssp/first</a>
</Link>
<br />
<Link href="/api/post/asdf">
<a id="to-api-post">to /api/post/[slug]</a>
</Link>
<br />
</>
)
}
Expand Down
10 changes: 10 additions & 0 deletions test/integration/i18n-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,16 @@ describe('i18n Support', () => {
expect(await browser.eval('window.location.pathname')).toBe(
`${localePath}gssp/first/`
)

await browser.back().waitForElementByCss('#index')
await browser.elementByCss('#to-api-post').click()

await browser.waitForCondition(
'window.location.pathname === "/api/post/asdf/"'
)
const body = await browser.elementByCss('body').text()
const json = JSON.parse(body)
expect(json.post).toBe(true)
}
})
}
Expand Down
3 changes: 3 additions & 0 deletions test/integration/middleware/core/pages/api/ok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function handler(req, res) {
res.send('ok')
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ export async function middleware(request) {
url.pathname = '/redirects/infinite-loop'
return Response.redirect(url)
}

if (url.pathname === '/redirects/to') {
url.pathname = url.searchParams.get('pathname')
url.searchParams.delete('pathname')
return Response.redirect(url)
}
}
4 changes: 4 additions & 0 deletions test/integration/middleware/core/pages/redirects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export default function Home() {
<a>Redirect me alot (infinite loop)</a>
</Link>
<div />
<Link href="/redirects/to?pathname=/api/ok" locale="nl">
<a id="link-to-api-with-locale">>Redirect me to api with locale</a>
</Link>
<div />
</div>
)
}

0 comments on commit 52b4214

Please sign in to comment.