Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Sep 19, 2023
1 parent 8856a71 commit c29b37b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async function checkEntrypoint(
entrypoint: string,
): Promise<
| { value: 'invalid' }
| { value: 'ignore', reason: string }
| { value: 'ignore'; reason: string }
| { value: 'valid'; finalEntrypoint: string }
> {
let finalEntrypoint = entrypoint;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gtr as versionGreaterThan, coerce } from 'semver';
import { cliError, cliWarn } from '../../cli';
import { getPackageVersion } from '../packageManagerUtils';
import { stripFuncExtension } from '../../utils';
import { formatRoutePath, stripFuncExtension } from '../../utils';
import type { CollectedFunctions, FunctionInfo } from './configs';
import { join, resolve } from 'path';
import type { ProcessVercelFunctionsOpts } from '.';
Expand Down Expand Up @@ -102,7 +102,7 @@ async function tryToFixI18nFunctions(

// Matches the format used in certain source route entries in the build output config.
// e.g. "src": "/(?<nextLocale>default|en|ja)(/.*|$)"
/\(\??<nextLocale>([^)]+)\)/
/\(\?<nextLocale>([^)]+)\)/
.exec(route.src)?.[1]
?.split('|')
?.forEach(locale => acc.add(locale));
Expand All @@ -127,12 +127,14 @@ async function tryToFixI18nFunctions(
.replace(new RegExp(`^/${i18nKey}/`), '/');
const fullPathWithoutI18nKey = join(functionsDir, pathWithoutI18nKey);

if (edgeFunctions.has(fullPathWithoutI18nKey)) {
const edgeFn = edgeFunctions.get(fullPathWithoutI18nKey);
if (edgeFn) {
invalidFunctions.delete(fullPath);
ignoredFunctions.set(fullPath, {
reason: 'unnecessary invalid i18n function',
...fnInfo,
});
edgeFn.route?.overrides?.push(formatRoutePath(fnInfo.relativePath));
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/next-on-pages/tests/_helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,8 @@ export async function collectFunctionsFrom(
});

await processOutputDir(outputDir, await getVercelStaticAssets());
const collectedFunctions = await collectFunctionConfigsRecursively(
functionsDir,
);
const collectedFunctions =
await collectFunctionConfigsRecursively(functionsDir);

return { collectedFunctions, restoreFsMock: () => mockFs.restore() };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ describe('checkInvalidFunctions', () => {
});
restoreFsMock();

const { edgeFunctions, invalidFunctions } = collectedFunctions;
const { edgeFunctions, invalidFunctions, ignoredFunctions } =
collectedFunctions;

expect(edgeFunctions.size).toEqual(1);
expect(getRouteInfo(edgeFunctions, 'index.func')).toEqual({
path: '/index',
overrides: ['/'],
overrides: ['/', '/en'],
});

expect(invalidFunctions.size).toEqual(0);

expect(ignoredFunctions.size).toEqual(1);
expect(ignoredFunctions.has(resolve(functionsDir, 'en.func'))).toEqual(
true,
);
});

test('should ignore i18n nested route with valid alternative', async () => {
Expand All @@ -63,15 +69,21 @@ describe('checkInvalidFunctions', () => {
});
restoreFsMock();

const { edgeFunctions, invalidFunctions } = collectedFunctions;
const { edgeFunctions, invalidFunctions, ignoredFunctions } =
collectedFunctions;

expect(edgeFunctions.size).toEqual(1);
expect(getRouteInfo(edgeFunctions, 'test/route.func')).toEqual({
path: '/test/route',
overrides: [],
overrides: ['/en/test/route'],
});

expect(invalidFunctions.size).toEqual(0);

expect(ignoredFunctions.size).toEqual(1);
expect(
ignoredFunctions.has(resolve(functionsDir, 'en/test/route.func')),
).toEqual(true);
});

test('should not ignore i18n with no valid alternative', async () => {
Expand All @@ -96,7 +108,8 @@ describe('checkInvalidFunctions', () => {
});
restoreFsMock();

const { edgeFunctions, invalidFunctions } = collectedFunctions;
const { edgeFunctions, invalidFunctions, ignoredFunctions } =
collectedFunctions;

expect(edgeFunctions.size).toEqual(0);

Expand All @@ -105,6 +118,8 @@ describe('checkInvalidFunctions', () => {
true,
);

expect(ignoredFunctions.size).toEqual(0);

expect(processExitMock).toHaveBeenCalledWith(1);
mockedConsole.expectCalls([
/The following routes were not configured to run with the Edge Runtime(?:.|\n)+- \/en/,
Expand Down

0 comments on commit c29b37b

Please sign in to comment.