8000 Experimental: Serverless Trace target by Timer · Pull Request #8246 · vercel/next.js · GitHub
[go: up one dir, main page]

Skip to content

Experimental: Serverless Trace target #8246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
8000
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add serverless trace tests
  • Loading branch information
Timer committed Aug 5, 2019
commit 2db38aa503fb29a71f03e5f044ae37828c779ed2
1 change: 1 addition & 0 deletions test/integration/serverless-trace/components/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <h1>Hello!</h1>
12 changes: 12 additions & 0 deletions test/integration/serverless-trace/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
target: 'experimental-serverless-trace',
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
},
experimental: {
publicDirectory: true
},
// make sure error isn't thrown from empty publicRuntimeConfig
publicRuntimeConfig: {}
}
1 change: 1 addition & 0 deletions test/integration/serverless-trace/pages/abc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <div>test</div>
3 changes: 3 additions & 0 deletions test/integration/serverless-trace/pages/api/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (req, res) => {
res.send('hello world')
}
3 changes: 3 additions & 0 deletions test/integration/serverless-trace/pages/api/posts/[id].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (req, res) => {
res.json({ post: req.query.id })
}
9 changes: 9 additions & 0 deletions test/integration/serverless-trace/pages/dynamic-two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import dynamic from 'next/dynamic'

const Hello = dynamic(() => import('../components/hello'))

export default () => (
<div>
<Hello />
</div>
)
9 changes: 9 additions & 0 deletions test/integration/serverless-trace/pages/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import dynamic from 'next/dynamic'

const Hello = dynamic(() => import('../components/hello'))

export default () => (
<div>
<Hello />
</div>
)
29 changes: 29 additions & 0 deletions test/integration/serverless-trace/pages/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fetch from 'isomorphic-unfetch'
import React from 'react'

export default class extends React.Component {
static async getInitialProps () {
try {
const res = await fetch('')
const text = await res.text()
console.log(text)
return { text }
} catch (err) {
if (err.message.includes('is not a function')) {
return { failed: true, error: err.toString() }
}

return { error: err.toString() }
}
}
render () {
const { failed, error, text } = this.props
return (
<div className='fetch-page'>
{failed ? 'failed' : ''}
{error}
<div id='text'>{text}</div>
</div>
)
}
}
11 changes: 11 additions & 0 deletions test/integration/serverless-trace/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Link from 'next/link'
export default () => {
return (
<div>
Hello World
<Link href='/fetch'>
<a id='fetchlink'>fetch page</a>
</Link>
</div>
)
}
2 changes: 2 additions & 0 deletions test/integration/serverless-trace/pages/some-amp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default () => `Hi Im an AMP page!`
export const config = { amp: 'hybrid' }
1 change: 1 addition & 0 deletions test/integration/serverless-trace/public/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
149 changes: 149 additions & 0 deletions test/integration/serverless-trace/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* eslint-env jest */
/* global jasmine */
import webdriver from 'next-webdriver'
import { join } from 'path'
import { existsSync } from 'fs'
import {
killApp,
findPort,
nextBuild,
nextStart,
fetchViaHTTP,
renderViaHTTP
} from 'next-test-utils'
import fetch from 'node-fetch'

const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages')
let appPort
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5

describe('Serverless', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})

it('should serve file from public folder', async () => {
const content = await renderViaHTTP(appPort, '/hello.txt')
expect(content.trim()).toBe('hello world')
})

it('should render the page with dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic')
expect(html).toMatch(/Hello!/)
})

it('should render the page with same dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic-two')
expect(html).toMatch(/Hello!/)
})

it('should render 404', async () => {
const html = await renderViaHTTP(appPort, '/404')
expect(html).toMatch(/This page could not be found/)
})

it('should render an AMP page', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/ampproject\.org/)
})

it('should have correct amphtml rel link', async () => {
const html = await renderViaHTTP(appPort, '/some-amp')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/rel="amphtml" href="\/some-amp\?amp=1"/)
})

it('should have correct canonical link', async () => {
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/rel="canonical" href="\/some-amp"/)
})

it('should render correctly when importing isomorphic-unfetch', async () => {
const url = `http://localhost:${appPort}/fetch`
const res = await fetch(url)
expect(res.status).toBe(200)
const text = await res.text()
expect(text.includes('failed')).toBe(false)
})

it('should render correctly when importing isomorphic-unfetch on the client side', async () => {
const browser = await webdriver(appPort, '/')
try {
const text = await browser
.elementByCss('a')
.click()
.waitForElementByCss('.fetch-page')
.elementByCss('#text')
.text()

expect(text).toMatch(/fetch page/)
} finally {
await browser.close()
}
})

it('should not output _app.js and _document.js to serverless build', () => {
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
})

it('should replace static pages with HTML files', async () => {
const staticFiles = ['abc', 'dynamic', 'dynamic-two', 'some-amp']
for (const file of staticFiles) {
expect(existsSync(join(serverlessDir, file + '.html'))).toBe(true)
expect(existsSync(join(serverlessDir, file + '.js'))).toBe(false)
}
})

it('should not replace non-static pages with HTML files', async () => {
const nonStaticFiles = ['fetch', '_error']
for (const file of nonStaticFiles) {
expect(existsSync(join(serverlessDir, file + '.js'))).toBe(true)
expect(existsSync(join(serverlessDir, file + '.html'))).toBe(false)
}
})

it('should reply on API request successfully', async () => {
const content = await renderViaHTTP(appPort, '/api/hello')
expect(content).toMatch(/hello world/)
})

it('should reply on dynamic API request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1')
const { post } = JSON.parse(result)
expect(post).toBe('post-1')
})

it('should 404 on API request with trailing slash', async () => {
const res = await fetchViaHTTP(appPort, '/api/hello/')
expect(res.status).toBe(404)
})

describe('With basic usage', () => {
it('should allow etag header support', async () => {
const url = `http://localhost:${appPort}/`
const etag = (await fetch(url)).headers.get('ETag')

const headers = { 'If-None-Match': etag }
const res2 = await fetch(url, { headers })
expect(res2.status).toBe(304)
})

it('should set Content-Length header', async () => {
const url = `http://localhost:${appPort}`
const res = await fetch(url)
expect(res.headers.get('Content-Length')).toBeDefined()
})
})
})
0