Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial head handling in app (#41607)
<!-- 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 that you're making: --> ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a 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 a 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/examples/adding-examples.md)
- Loading branch information
Showing
19 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import path from 'path' | ||
import cheerio from 'cheerio' | ||
import { createNext, FileRef } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { renderViaHTTP } from 'next-test-utils' | ||
|
||
describe('app dir head', () => { | ||
if ((global as any).isNextDeploy) { | ||
it('should skip next deploy for now', () => {}) | ||
return | ||
} | ||
|
||
if (process.env.NEXT_TEST_REACT_VERSION === '^17') { | ||
it('should skip for react v17', () => {}) | ||
return | ||
} | ||
let next: NextInstance | ||
|
||
function runTests() { | ||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: new FileRef(path.join(__dirname, 'head')), | ||
dependencies: { | ||
react: 'experimental', | ||
'react-dom': 'experimental', | ||
}, | ||
skipStart: true, | ||
}) | ||
|
||
await next.start() | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('should use head from index page', async () => { | ||
const html = await renderViaHTTP(next.url, '/') | ||
const $ = cheerio.load(html) | ||
const headTags = $('head').children().toArray() | ||
|
||
expect(headTags.find((el) => el.attribs.src === '/hello.js')).toBeTruthy() | ||
expect( | ||
headTags.find((el) => el.attribs.src === '/another.js') | ||
).toBeTruthy() | ||
}) | ||
|
||
it('should use correct head for /blog', async () => { | ||
const html = await renderViaHTTP(next.url, '/blog') | ||
const $ = cheerio.load(html) | ||
const headTags = $('head').children().toArray() | ||
|
||
expect(headTags.find((el) => el.attribs.src === '/hello3.js')).toBeFalsy() | ||
expect( | ||
headTags.find((el) => el.attribs.src === '/hello1.js') | ||
).toBeTruthy() | ||
expect( | ||
headTags.find((el) => el.attribs.src === '/hello2.js') | ||
).toBeTruthy() | ||
expect( | ||
headTags.find((el) => el.attribs.src === '/another.js') | ||
).toBeTruthy() | ||
}) | ||
|
||
it('should use head from layout when not on page', async () => { | ||
const html = await renderViaHTTP(next.url, '/blog/about') | ||
const $ = cheerio.load(html) | ||
const headTags = $('head').children().toArray() | ||
|
||
expect( | ||
headTags.find((el) => el.attribs.src === '/hello1.js') | ||
).toBeTruthy() | ||
expect( | ||
headTags.find((el) => el.attribs.src === '/hello2.js') | ||
).toBeTruthy() | ||
expect( | ||
headTags.find((el) => el.attribs.src === '/another.js') | ||
).toBeTruthy() | ||
}) | ||
|
||
it('should pass params to head for dynamic path', async () => { | ||
const html = await renderViaHTTP(next.url, '/blog/post-1') | ||
const $ = cheerio.load(html) | ||
const headTags = $('head').children().toArray() | ||
|
||
expect( | ||
headTags.find( | ||
(el) => | ||
el.attribs.src === '/hello3.js' && | ||
el.attribs['data-slug'] === 'post-1' | ||
) | ||
).toBeTruthy() | ||
expect( | ||
headTags.find((el) => el.attribs.src === '/another.js') | ||
).toBeTruthy() | ||
}) | ||
} | ||
|
||
runTests() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default async function Head({ params }) { | ||
return ( | ||
<> | ||
<script async src="/hello3.js" data-slug={params.slug} /> | ||
<title>{`hello from dynamic blog page ${params.slug}`}</title> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<p id="page">dynamic blog page</p> | ||
<Link href="/" id="to-index"> | ||
to / | ||
</Link> | ||
<br /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<p id="page">blog about page</p> | ||
<Link href="/" id="to-index"> | ||
to / | ||
</Link> | ||
<br /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default async function Head() { | ||
return ( | ||
<> | ||
<script async src="/hello1.js" /> | ||
<script async src="/hello2.js" /> | ||
<title>hello from blog layout</title> | ||
<meta name="description" content="a neat blog" /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function Layout({ children }) { | ||
return ( | ||
<> | ||
<p id="layout">blog layout</p> | ||
{children} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<p id="page">blog page</p> | ||
<Link href="/" id="to-index"> | ||
to / | ||
</Link> | ||
<br /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default async function Head() { | ||
return ( | ||
<> | ||
<script async src="/hello.js" /> | ||
<title>hello from index</title> | ||
<meta name="description" content="an index page" /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default function Root({ children }) { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<script async src="/another.js" /> | ||
</head> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<p id="page">index page</p> | ||
|
||
<Link href="/blog" id="to-blog"> | ||
to /blog | ||
</Link> | ||
<br /> | ||
|
||
<Link href="/blog/post-1" id="to-blog-slug"> | ||
to /blog/post-1 | ||
</Link> | ||
<br /> | ||
|
||
<Link href="/blog/about" id="to-blog-about"> | ||
to /blog/about | ||
</Link> | ||
<br /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = { | ||
experimental: { | ||
appDir: true, | ||
}, | ||
// assetPrefix: '/assets', | ||
rewrites: async () => { | ||
return { | ||
// beforeFiles: [ { source: '/assets/:path*', destination: '/:path*' } ], | ||
afterFiles: [ | ||
{ | ||
source: '/rewritten-to-dashboard', | ||
destination: '/dashboard', | ||
}, | ||
], | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
if (typeof window !== 'undefined') { | ||
window.fromAnother = true | ||
} | ||
console.log('another') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
if (typeof window !== 'undefined') { | ||
window.fromHello = true | ||
} | ||
console.log('hello') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
if (typeof window !== 'undefined') { | ||
window.fromHello1 = true | ||
} | ||
console.log('hello1') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
if (typeof window !== 'undefined') { | ||
window.fromHello2 = true | ||
} | ||
console.log('hello2') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
if (typeof window !== 'undefined') { | ||
window.fromHello3 = true | ||
} | ||
console.log('hello3') |