Skip to content
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

Fix index.js for API routes #8112

Merged
merged 3 commits into from Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/next/build/entries.ts
@@ -1,6 +1,6 @@
import { join } from 'path'
import { stringify } from 'querystring'
import { PAGES_DIR_ALIAS, DOT_NEXT_ALIAS } from '../lib/constants'
import { PAGES_DIR_ALIAS, DOT_NEXT_ALIAS, API_ROUTE } from '../lib/constants'
import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'

type PagesMapping = {
Expand Down Expand Up @@ -68,7 +68,7 @@ export function createEntrypoints(
Object.keys(pages).forEach(page => {
const absolutePagePath = pages[page]
const bundleFile = page === '/' ? '/index.js' : `${page}.js`
const isApiRoute = bundleFile.startsWith('/api/')
const isApiRoute = bundleFile.match(API_ROUTE)

const bundlePath = join('static', buildId, 'pages', bundleFile)

Expand Down
Expand Up @@ -3,6 +3,7 @@ import { join } from 'path'
import { parse } from 'querystring'
import { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST } from 'next-server/constants'
import { isDynamicRoute } from 'next-server/dist/lib/router/utils'
import { API_ROUTE } from '../../../lib/constants'

export type ServerlessLoaderQuery = {
page: string
Expand Down Expand Up @@ -39,7 +40,7 @@ const nextServerlessLoader: loader.Loader = function() {
'/'
)

if (page.startsWith('/api/')) {
if (page.match(API_ROUTE)) {
return `
${
isDynamicRoute(page)
Expand Down
3 changes: 3 additions & 0 deletions packages/next/lib/constants.ts
Expand Up @@ -15,6 +15,9 @@ export const NEXT_PROJECT_ROOT_DIST_SERVER = join(
'server'
)

// Regex for API routes
export const API_ROUTE = /^\/api(?:\/|$)/

// Because on Windows absolute paths in the generated code can break because of numbers, eg 1 in the path,
// we have to use a private alias
export const PAGES_DIR_ALIAS = 'private-next-pages'
Expand Down
3 changes: 3 additions & 0 deletions test/integration/api-support/pages/api/index.js
@@ -0,0 +1,3 @@
export default (req, res) => {
res.send('Index should work')
}
21 changes: 21 additions & 0 deletions test/integration/api-support/test/index.test.js
Expand Up @@ -45,6 +45,27 @@ function runTests (serverless = false) {
killApp(app)
})

it('should work with index api', async () => {
if (serverless) {
const port = await findPort()
const resolver = require(join(appDir, '.next/serverless/pages/api.js'))
.default

const server = createServer(resolver).listen(port)
const res = await fetchViaHTTP(port, '/api')
const text = await res.text()
server.close()

expect(text).toEqual('Index should work')
} else {
const text = await fetchViaHTTP(appPort, '/api', null, {}).then(
res => res.ok && res.text()
)

expect(text).toEqual('Index should work')
}
})

it('should return custom error', async () => {
const data = await fetchViaHTTP(appPort, '/api/error', null, {})
const json = await data.json()
Expand Down