Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
ssr-site: use fallback origin to get around top level static asset limit
Browse files Browse the repository at this point in the history
closes #1041
  • Loading branch information
fwang committed Sep 19, 2024
1 parent 64442a5 commit 47bd118
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 168 deletions.
15 changes: 6 additions & 9 deletions platform/src/components/aws/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ export class Astro extends Component implements Link.Linkable {

plan.behaviors.push(
{
cacheType: "server",
cfFunction: "serverHostOnly",
origin: "regionalServer",
},
{
pattern: "*",
cacheType: "server",
cfFunction: "server",
origin: "fallthroughServer",
Expand All @@ -567,15 +573,6 @@ export class Astro extends Component implements Link.Linkable {
origin: "regionalServer",
allowedMethods: ["GET", "HEAD", "OPTIONS"],
},
...buildMeta.serverRoutes?.map(
(route) =>
({
cacheType: "server",
cfFunction: "serverHostOnly",
pattern: route,
origin: "regionalServer",
}) as const,
),
);
}

Expand Down
36 changes: 13 additions & 23 deletions platform/src/components/aws/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,19 +438,9 @@ export class Nuxt extends Component implements Link.Linkable {
}

function loadBuildMetadata() {
return outputPath.apply((outputPath) => {
const assetsPath = path.join(".output", "public");

return {
assetsPath,
// create 1 behaviour for each top level asset file/folder
staticRoutes: fs
.readdirSync(path.join(outputPath, assetsPath), {
withFileTypes: true,
})
.map((item) => (item.isDirectory() ? `${item.name}/*` : item.name)),
};
});
return outputPath.apply(() => ({
assetsPath: path.join(".output", "public"),
}));
}

function buildPlan() {
Expand Down Expand Up @@ -485,6 +475,13 @@ export class Nuxt extends Component implements Link.Linkable {
],
},
},
fallthrough: {
group: {
primaryOriginName: "s3",
fallbackOriginName: "server",
fallbackStatusCodes: [403, 404],
},
},
},
behaviors: [
{
Expand All @@ -493,19 +490,12 @@ export class Nuxt extends Component implements Link.Linkable {
origin: "server",
},
{
pattern: "_server/",
pattern: "*",
cacheType: "server",
cfFunction: "serverCfFunction",
origin: "server",
origin: "fallthrough",
allowedMethods: ["GET", "HEAD", "OPTIONS"],
},
...buildMeta.staticRoutes.map(
(route) =>
({
cacheType: "static",
pattern: route,
origin: "s3",
}) as const,
),
],
});
});
Expand Down
36 changes: 13 additions & 23 deletions platform/src/components/aws/solid-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,19 +445,9 @@ export class SolidStart extends Component implements Link.Linkable {
}

function loadBuildMetadata() {
return outputPath.apply((outputPath) => {
const assetsPath = path.join(".output", "public");

return {
assetsPath,
// create 1 behaviour for each top level asset file/folder
staticRoutes: fs
.readdirSync(path.join(outputPath, assetsPath), {
withFileTypes: true,
})
.map((item) => (item.isDirectory() ? `${item.name}/*` : item.name)),
};
});
return outputPath.apply(() => ({
assetsPath: path.join(".output", "public"),
}));
}

function buildPlan() {
Expand Down Expand Up @@ -494,6 +484,13 @@ export class SolidStart extends Component implements Link.Linkable {
],
},
},
fallthrough: {
group: {
primaryOriginName: "s3",
fallbackOriginName: "server",
fallbackStatusCodes: [403, 404],
},
},
},
behaviors: [
{
Expand All @@ -502,19 +499,12 @@ export class SolidStart extends Component implements Link.Linkable {
origin: "server",
},
{
pattern: "_server/",
pattern: "*",
cacheType: "server",
cfFunction: "serverCfFunction",
origin: "server",
origin: "fallthrough",
allowedMethods: ["GET", "HEAD", "OPTIONS"],
},
...buildMeta.staticRoutes.map(
(route) =>
({
cacheType: "static",
pattern: route,
origin: "s3",
}) as const,
),
],
});
},
Expand Down
189 changes: 76 additions & 113 deletions platform/src/components/aws/svelte-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,6 @@ export interface SvelteKitArgs extends SsrSiteArgs {
* ```
*/
assets?: SsrSiteArgs["assets"];
/**
* Configure where the [server function](#nodes-server) is deployed.
*
* By default, it's deployed to AWS Lambda in a single region. Enable this option if you want to instead deploy it to Lambda@Edge.
* @default `false`
* @internal
*/
edge?: Input<boolean>;
/**
* Configure the [server function](#nodes-server) in your SvelteKit app to connect
* to private subnets in a virtual private cloud or VPC. This allows your app to
Expand Down Expand Up @@ -359,7 +351,6 @@ export class SvelteKit extends Component implements Link.Linkable {
super(__pulumiType, name, args, opts);

const parent = this;
const edge = normalizeEdge();
const { sitePath, partition } = prepare(parent, args);
const dev = normalizeDev();

Expand All @@ -370,7 +361,6 @@ export class SvelteKit extends Component implements Link.Linkable {
_metadata: {
mode: "placeholder",
path: sitePath,
edge,
server: server.arn,
},
_receiver: {
Expand Down Expand Up @@ -426,7 +416,6 @@ export class SvelteKit extends Component implements Link.Linkable {
mode: "deployed",
path: sitePath,
url: distribution.apply((d) => d.domainUrl ?? d.url),
edge,
server: serverFunction.arn,
},
});
Expand All @@ -444,15 +433,11 @@ export class SvelteKit extends Component implements Link.Linkable {
};
}

function normalizeEdge() {
return output(args?.edge).apply((edge) => edge ?? false);
}

function loadBuildMetadata() {
const serverPath = ".svelte-kit/svelte-kit-sst/server";
const assetsPath = ".svelte-kit/svelte-kit-sst/client";

return outputPath.apply((outputPath) => {
return outputPath.apply(() => {
let basePath = "";
try {
const manifest = fs
Expand All @@ -472,117 +457,95 @@ export class SvelteKit extends Component implements Link.Linkable {
prerenderedPath: ".svelte-kit/svelte-kit-sst/prerendered",
assetsPath,
assetsVersionedSubDir: "_app",
// create 1 behaviour for each top level asset file/folder
staticRoutes: fs
.readdirSync(path.join(outputPath, assetsPath), {
withFileTypes: true,
})
.map((item) =>
item.isDirectory()
? `${basePath}${item.name}/*`
: `${basePath}${item.name}`,
),
};
});
}

function buildPlan() {
return all([outputPath, edge, buildMeta]).apply(
([outputPath, edge, buildMeta]) => {
const serverConfig = {
handler: path.join(
outputPath,
buildMeta.serverPath,
"lambda-handler",
"index.handler",
),
nodejs: {
esbuild: {
minify: process.env.SST_DEBUG ? false : true,
sourcemap: process.env.SST_DEBUG ? ("inline" as const) : false,
define: {
"process.env.SST_DEBUG": process.env.SST_DEBUG
? "true"
: "false",
},
return all([outputPath, buildMeta]).apply(([outputPath, buildMeta]) => {
const serverConfig = {
handler: path.join(
outputPath,
buildMeta.serverPath,
"lambda-handler",
"index.handler",
),
nodejs: {
esbuild: {
minify: process.env.SST_DEBUG ? false : true,
sourcemap: process.env.SST_DEBUG ? ("inline" as const) : false,
define: {
"process.env.SST_DEBUG": process.env.SST_DEBUG
? "true"
: "false",
},
},
copyFiles: buildMeta.serverFiles
? [
},
copyFiles: buildMeta.serverFiles
? [
{
from: path.join(outputPath, buildMeta.serverFiles),
to: "prerendered",
},
]
: undefined,
};

return validatePlan({
edge: false,
cloudFrontFunctions: {
serverCfFunction: {
injections: [
useCloudFrontFunctionHostHeaderInjection(),
useCloudFrontFormActionInjection(),
],
},
},
origins: {
server: {
server: { function: serverConfig },
},
s3: {
s3: {
copy: [
{
from: path.join(outputPath, buildMeta.serverFiles),
to: "prerendered",
from: buildMeta.assetsPath,
to: buildMeta.basePath,
cached: true,
versionedSubDir: buildMeta.assetsVersionedSubDir,
},
{
from: buildMeta.prerenderedPath,
to: buildMeta.basePath,
cached: false,
},
]
: undefined,
};

return validatePlan({
edge,
cloudFrontFunctions: {
serverCfFunction: {
injections: [
useCloudFrontFunctionHostHeaderInjection(),
useCloudFrontFormActionInjection(),
],
},
},
edgeFunctions: edge
? {
server: { function: serverConfig },
}
: undefined,
origins: {
...(edge
? {}
: {
server: {
server: { function: serverConfig },
},
}),
s3: {
s3: {
copy: [
{
from: buildMeta.assetsPath,
to: buildMeta.basePath,
cached: true,
versionedSubDir: buildMeta.assetsVersionedSubDir,
},
{
from: buildMeta.prerenderedPath,
to: buildMeta.basePath,
cached: false,
},
],
},
fallthrough: {
group: {
primaryOriginName: "s3",
fallbackOriginName: "server",
fallbackStatusCodes: [403, 404],
},
},
behaviors: [
edge
? {
cacheType: "server",
cfFunction: "serverCfFunction",
edgeFunction: "server",
origin: "s3",
}
: {
cacheType: "server",
cfFunction: "serverCfFunction",
origin: "server",
},
...buildMeta.staticRoutes.map(
(route) =>
({
cacheType: "static",
pattern: route,
origin: "s3",
}) as const,
),
],
});
},
);
},
behaviors: [
{
cacheType: "server",
cfFunction: "serverCfFunction",
origin: "server",
},
{
pattern: "*",
cacheType: "server",
cfFunction: "serverCfFunction",
origin: "fallthrough",
allowedMethods: ["GET", "HEAD", "OPTIONS"],
},
],
});
});
}
}

Expand Down

0 comments on commit 47bd118

Please sign in to comment.