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

Proxy: Fix root route rewrites #141

Merged
merged 2 commits into from
Jun 19, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ resource "aws_cloudfront_cache_policy" "this" {
locals {
# CloudFront default root object
################################
cloudfront_default_root_object = "index"
cloudfront_default_root_object = ""

# CloudFront Origins
####################
Expand Down
3 changes: 1 addition & 2 deletions packages/proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"dependencies": {
"@vercel/routing-utils": "^1.9.1",
"abort-controller": "^3.0.0",
"node-fetch": "^2.6.1",
"pcre-to-regexp": "^1.1.0"
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.76",
Expand Down
277 changes: 277 additions & 0 deletions packages/proxy/src/__test__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,4 +545,281 @@ describe('[proxy] Handler', () => {
},
TIMEOUT
);

test(
'i18n default locale rewrite',
async () => {
const proxyConfig: ProxyConfig = {
lambdaRoutes: [],
prerenders: {},
staticRoutes: [],
routes: [
{
src: '^/(?!(?:_next/.*|en|fr\\-FR|nl)(?:/.*|$))(.*)$',
dest: '$wildcard/$1',
continue: true,
},
{
src: '/',
locale: {
redirect: {
en: '/',
'fr-FR': '/fr-FR',
nl: '/nl',
},
cookie: 'NEXT_LOCALE',
},
continue: true,
},
{
src: '^/$',
dest: '/en',
continue: true,
},
],
};
const requestPath = '/';

// Prepare configServer
configServer.proxyConfig = proxyConfig;

// Origin Request
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#example-origin-request
const event: CloudFrontRequestEvent = {
Records: [
{
cf: {
config: {
distributionDomainName: 'd111111abcdef8.cloudfront.net',
distributionId: 'EDFDVBD6EXAMPLE',
eventType: 'origin-request',
requestId:
'4TyzHTaYWb1GX1qTfsHhEqV6HUDd_BzoBZnwfnvQc_1oF26ClkoUSEQ==',
},
request: {
clientIp: '203.0.113.178',
headers: {
'x-forwarded-for': [
{
key: 'X-Forwarded-For',
value: '203.0.113.178',
},
],
'user-agent': [
{
key: 'User-Agent',
value: 'Amazon CloudFront',
},
],
via: [
{
key: 'Via',
value:
'2.0 2afae0d44e2540f472c0635ab62c232b.cloudfront.net (CloudFront)',
},
],
host: [
{
key: 'Host',
value: 'example.org',
},
],
'cache-control': [
{
key: 'Cache-Control',
value: 'no-cache, cf-no-cache',
},
],
},
method: 'GET',
origin: {
s3: {
customHeaders: {
'x-env-config-endpoint': [
{
key: 'x-env-config-endpoint',
value: configEndpoint,
},
],
'x-env-api-endpoint': [
{
key: 'x-env-api-endpoint',
value: 'example.localhost',
},
],
},
region: 'us-east-1',
authMethod: 'origin-access-identity',
domainName: 's3.localhost',
path: '',
},
},
querystring: '',
uri: requestPath,
},
},
},
],
};

const result = (await handler(event)) as CloudFrontRequest;

expect(result.origin?.s3).toEqual(
expect.objectContaining({
domainName: 's3.localhost',
path: '',
})
);
expect(result.uri).toBe('/en');
},
TIMEOUT
);

test(
'Correctly request /index object from S3 when requesting /',
async () => {
const proxyConfig: ProxyConfig = {
staticRoutes: ['/404', '/500', '/index'],
lambdaRoutes: [],
routes: [
{
src: '^(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))\\/$',
headers: {
Location: '/$1',
},
status: 308,
continue: true,
},
{
src: '/404',
status: 404,
continue: true,
},
{
handle: 'filesystem',
},
{
handle: 'resource',
},
{
src: '/.*',
status: 404,
},
{
handle: 'miss',
},
{
handle: 'rewrite',
},
{
handle: 'hit',
},
{
handle: 'error',
},
{
src: '/.*',
dest: '/404',
status: 404,
},
],
prerenders: {},
};

const requestPath = '/';

// Prepare configServer
configServer.proxyConfig = proxyConfig;

// Origin Request
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#example-origin-request
const event: CloudFrontRequestEvent = {
Records: [
{
cf: {
config: {
distributionDomainName: 'd111111abcdef8.cloudfront.net',
distributionId: 'EDFDVBD6EXAMPLE',
eventType: 'origin-request',
requestId:
'4TyzHTaYWb1GX1qTfsHhEqV6HUDd_BzoBZnwfnvQc_1oF26ClkoUSEQ==',
},
request: {
clientIp: '203.0.113.178',
headers: {
'x-forwarded-for': [
{
key: 'X-Forwarded-For',
value: '203.0.113.178',
},
],
'user-agent': [
{
key: 'User-Agent',
value: 'Amazon CloudFront',
},
],
via: [
{
key: 'Via',
value:
'2.0 2afae0d44e2540f472c0635ab62c232b.cloudfront.net (CloudFront)',
},
],
host: [
{
key: 'Host',
value: 'example.org',
},
],
'cache-control': [
{
key: 'Cache-Control',
value: 'no-cache, cf-no-cache',
},
],
},
method: 'GET',
origin: {
s3: {
customHeaders: {
'x-env-config-endpoint': [
{
key: 'x-env-config-endpoint',
value: configEndpoint,
},
],
'x-env-api-endpoint': [
{
key: 'x-env-api-endpoint',
value: 'example.localhost',
},
],
},
region: 'us-east-1',
authMethod: 'origin-access-identity',
domainName: 's3.localhost',
path: '',
},
},
querystring: '',
uri: requestPath,
},
},
},
],
};

const result = (await handler(event)) as CloudFrontRequest;

expect(result.origin?.s3).toEqual(
expect.objectContaining({
domainName: 's3.localhost',
path: '',
})
);
expect(result.uri).toBe('/index');
},
TIMEOUT
);
});
71 changes: 71 additions & 0 deletions packages/proxy/src/__test__/proxy.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,74 @@ test('[proxy-unit] External rewrite', () => {
target: 'url',
});
});

test('[proxy-unit] Rewrite with ^ and $', () => {
const routesConfig = [
{
src: '^/$',
dest: '/en',
continue: true,
},
] as Route[];

const result = new Proxy(routesConfig, [], []).route('/');

expect(result).toEqual({
found: true,
dest: '/en',
continue: true,
status: undefined,
headers: {},
uri_args: new URLSearchParams(),
matched_route: routesConfig[0],
matched_route_idx: 0,
userDest: true,
isDestUrl: false,
phase: undefined,
target: undefined,
});
});

test('[proxy-unit] i18n default locale', () => {
const routesConfig = [
{
src: '^/(?!(?:_next/.*|en|fr\\-FR|nl)(?:/.*|$))(.*)$',
dest: '$wildcard/$1',
continue: true,
},
{
src: '/',
locale: {
redirect: {
en: '/',
'fr-FR': '/fr-FR',
nl: '/nl',
},
cookie: 'NEXT_LOCALE',
},
continue: true,
},
{
src: '^/$',
dest: '/en',
continue: true,
},
] as Route[];

const result = new Proxy(routesConfig, [], []).route('/');

expect(result).toEqual({
found: true,
dest: '/en',
continue: true,
status: undefined,
headers: {},
uri_args: new URLSearchParams(''),
matched_route: routesConfig[2],
matched_route_idx: 2,
userDest: true,
isDestUrl: false,
phase: undefined,
target: undefined,
});
});
Loading