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

feat: add path to manifest #455

Merged
merged 3 commits into from
Aug 17, 2023
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
49 changes: 42 additions & 7 deletions node/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,50 @@ test('Loads function paths from the in-source `config` function', async () => {
expect(generatedFiles.includes(bundles[0].asset)).toBe(true)

expect(routes.length).toBe(6)
expect(routes[0]).toEqual({ function: 'framework-func2', pattern: '^/framework-func2/?$', excluded_patterns: [] })
expect(routes[1]).toEqual({ function: 'user-func2', pattern: '^/user-func2/?$', excluded_patterns: [] })
expect(routes[2]).toEqual({ function: 'framework-func1', pattern: '^/framework-func1/?$', excluded_patterns: [] })
expect(routes[3]).toEqual({ function: 'user-func1', pattern: '^/user-func1/?$', excluded_patterns: [] })
expect(routes[4]).toEqual({ function: 'user-func3', pattern: '^/user-func3/?$', excluded_patterns: [] })
expect(routes[5]).toEqual({ function: 'user-func5', pattern: '^/user-func5(?:/(.*))/?$', excluded_patterns: [] })
expect(routes[0]).toEqual({
function: 'framework-func2',
pattern: '^/framework-func2/?$',
excluded_patterns: [],
path: '/framework-func2',
})
expect(routes[1]).toEqual({
function: 'user-func2',
pattern: '^/user-func2/?$',
excluded_patterns: [],
path: '/user-func2',
})
expect(routes[2]).toEqual({
function: 'framework-func1',
pattern: '^/framework-func1/?$',
excluded_patterns: [],
path: '/framework-func1',
})
expect(routes[3]).toEqual({
function: 'user-func1',
pattern: '^/user-func1/?$',
excluded_patterns: [],
path: '/user-func1',
})
expect(routes[4]).toEqual({
function: 'user-func3',
pattern: '^/user-func3/?$',
excluded_patterns: [],
path: '/user-func3',
})
expect(routes[5]).toEqual({
function: 'user-func5',
pattern: '^/user-func5(?:/(.*))/?$',
excluded_patterns: [],
path: '/user-func5/*',
})

expect(postCacheRoutes.length).toBe(1)
expect(postCacheRoutes[0]).toEqual({ function: 'user-func4', pattern: '^/user-func4/?$', excluded_patterns: [] })
expect(postCacheRoutes[0]).toEqual({
function: 'user-func4',
pattern: '^/user-func4/?$',
excluded_patterns: [],
path: '/user-func4',
})

expect(Object.keys(functionConfig)).toHaveLength(1)
expect(functionConfig['user-func5']).toEqual({
Expand Down
38 changes: 24 additions & 14 deletions node/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('Generates a manifest with different bundles', () => {
{ asset: bundle1.hash + bundle1.extension, format: bundle1.format },
{ asset: bundle2.hash + bundle2.extension, format: bundle2.format },
]
const expectedRoutes = [{ function: 'func-1', pattern: '^/f1/?$', excluded_patterns: [] }]
const expectedRoutes = [{ function: 'func-1', pattern: '^/f1/?$', excluded_patterns: [], path: '/f1' }]

expect(manifest.bundles).toEqual(expectedBundles)
expect(manifest.routes).toEqual(expectedRoutes)
Expand All @@ -53,7 +53,7 @@ test('Generates a manifest with display names', () => {
featureFlags: { edge_functions_path_urlpattern: true },
})

const expectedRoutes = [{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: [] }]
const expectedRoutes = [{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: [], path: '/f1/*' }]
expect(manifest.function_config).toEqual({
'func-1': { name: 'Display Name' },
})
Expand All @@ -78,7 +78,7 @@ test('Generates a manifest with a generator field', () => {
featureFlags: { edge_functions_path_urlpattern: true },
})

const expectedRoutes = [{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: [] }]
const expectedRoutes = [{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: [], path: '/f1/*' }]
const expectedFunctionConfig = { 'func-1': { generator: '@netlify/fake-plugin@1.0.0' } }
expect(manifest.routes).toEqual(expectedRoutes)
expect(manifest.function_config).toEqual(expectedFunctionConfig)
Expand All @@ -102,12 +102,17 @@ test('Generates a manifest with excluded paths and patterns', () => {
featureFlags: { edge_functions_path_urlpattern: true },
})
const expectedRoutes = [
{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: ['^/f1/exclude/?$'] },
{ function: 'func-2', pattern: '^/f2(?:/(.*))/?$', excluded_patterns: ['^/f2/exclude$', '^/f2/exclude-as-well$'] },
{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: ['^/f1/exclude/?$'], path: '/f1/*' },
{
function: 'func-2',
pattern: '^/f2(?:/(.*))/?$',
excluded_patterns: ['^/f2/exclude$', '^/f2/exclude-as-well$'],
},
{
function: 'func-3',
pattern: '^(?:/(.*))/?$',
excluded_patterns: ['^(?:/((?:.*)(?:/(?:.*))*))?(?:/(.*))\\.html/?$'],
path: '/*',
},
]

Expand All @@ -134,7 +139,7 @@ test('TOML-defined paths can be combined with ISC-defined excluded paths', () =>
userFunctionConfig,
featureFlags: { edge_functions_path_urlpattern: true },
})
const expectedRoutes = [{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: [] }]
const expectedRoutes = [{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: [], path: '/f1/*' }]

expect(manifest.routes).toEqual(expectedRoutes)
expect(manifest.function_config).toEqual({
Expand Down Expand Up @@ -219,11 +224,13 @@ test('excludedPath from ISC goes into function_config, TOML goes into routes', (
function: 'customisation',
pattern: '^/showcases(?:/(.*))/?$',
excluded_patterns: [],
path: '/showcases/*',
},
{
function: 'customisation',
pattern: '^/checkout(?:/(.*))/?$',
excluded_patterns: ['^(?:/(.*))/terms-and-conditions/?$'],
path: '/checkout/*',
},
])
expect(manifest.function_config).toEqual({
Expand Down Expand Up @@ -259,6 +266,7 @@ test('URLPattern named groups are supported', () => {
function: 'customisation',
pattern: '^/products(?:/([^/]+?))/?$',
excluded_patterns: [],
path: '/products/:productId',
},
])

Expand Down Expand Up @@ -318,7 +326,7 @@ test('Excludes functions for which there are function files but no matching conf
const declarations: Declaration[] = [{ function: 'func-1', path: '/f1' }]
const manifest = generateManifest({ bundles: [bundle1], declarations, functions })

const expectedRoutes = [{ function: 'func-1', pattern: '^/f1/?$', excluded_patterns: [] }]
const expectedRoutes = [{ function: 'func-1', pattern: '^/f1/?$', excluded_patterns: [], path: '/f1' }]

expect(manifest.routes).toEqual(expectedRoutes)
})
Expand All @@ -336,7 +344,7 @@ test('Excludes functions for which there are config declarations but no matching
]
const manifest = generateManifest({ bundles: [bundle1], declarations, functions })

const expectedRoutes = [{ function: 'func-2', pattern: '^/f2/?$', excluded_patterns: [] }]
const expectedRoutes = [{ function: 'func-2', pattern: '^/f2/?$', excluded_patterns: [], path: '/f2' }]

expect(manifest.routes).toEqual(expectedRoutes)
})
Expand All @@ -346,7 +354,7 @@ test('Generates a manifest without bundles', () => {
const declarations: Declaration[] = [{ function: 'func-1', path: '/f1' }]
const manifest = generateManifest({ bundles: [], declarations, functions })

const expectedRoutes = [{ function: 'func-1', pattern: '^/f1/?$', excluded_patterns: [] }]
const expectedRoutes = [{ function: 'func-1', pattern: '^/f1/?$', excluded_patterns: [], path: '/f1' }]

expect(manifest.bundles).toEqual([])
expect(manifest.routes).toEqual(expectedRoutes)
Expand Down Expand Up @@ -381,10 +389,12 @@ test('Generates a manifest with pre and post-cache routes', () => {
{ asset: bundle2.hash + bundle2.extension, format: bundle2.format },
]
const expectedPreCacheRoutes = [
{ function: 'func-1', name: undefined, pattern: '^/f1/?$', excluded_patterns: [] },
{ function: 'func-2', name: undefined, pattern: '^/f2/?$', excluded_patterns: [] },
{ function: 'func-1', name: undefined, pattern: '^/f1/?$', excluded_patterns: [], path: '/f1' },
{ function: 'func-2', name: undefined, pattern: '^/f2/?$', excluded_patterns: [], path: '/f2' },
]
const expectedPostCacheRoutes = [
{ function: 'func-3', name: undefined, pattern: '^/f3/?$', excluded_patterns: [], path: '/f3' },
]
const expectedPostCacheRoutes = [{ function: 'func-3', name: undefined, pattern: '^/f3/?$', excluded_patterns: [] }]

expect(manifest.bundles).toEqual(expectedBundles)
expect(manifest.routes).toEqual(expectedPreCacheRoutes)
Expand All @@ -402,8 +412,8 @@ test('Generates a manifest with layers', () => {
{ function: 'func-2', path: '/f2/*' },
]
const expectedRoutes = [
{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: [] },
{ function: 'func-2', pattern: '^/f2(?:/(.*))/?$', excluded_patterns: [] },
{ function: 'func-1', pattern: '^/f1(?:/(.*))/?$', excluded_patterns: [], path: '/f1/*' },
{ function: 'func-2', pattern: '^/f2(?:/(.*))/?$', excluded_patterns: [], path: '/f2/*' },
]
const layers = [
{
Expand Down
5 changes: 5 additions & 0 deletions node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Route {
function: string
pattern: string
excluded_patterns: string[]
path?: string
}

interface EdgeFunctionConfig {
Expand Down Expand Up @@ -141,6 +142,10 @@ const generateManifest = ({
excluded_patterns: excludedPattern.map(serializePattern),
}

if ('path' in declaration) {
route.path = declaration.path
}

if (declaration.cache === Cache.Manual) {
postCacheRoutes.push(route)
} else {
Expand Down
1 change: 1 addition & 0 deletions node/validation/manifest/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const routesSchema = {
},
excluded_patterns: excludedPatternsSchema,
generator: { type: 'string' },
path: { type: 'string' },
},
additionalProperties: false,
}
Expand Down