Skip to content

Commit e97d5d5

Browse files
author
James Bray
committed
Move to lambda at edge based routing
1 parent 2d86ac9 commit e97d5d5

File tree

7 files changed

+14468
-12
lines changed

7 files changed

+14468
-12
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# adapter-lambda for SvelteKit
22

33
An adapter to build a [SvelteKit](https://kit.svelte.dev/) app into a lambda ready for deployment with lambda proxy.
4+
5+
## Installation
46
```
57
npm install --save-dev @yarbsemaj/adapter-lambda
68
```
7-
89
## Usage
910

1011
In your `svelte.config.cjs` configure the adapter as bellow;
@@ -24,5 +25,11 @@ const config = {
2425

2526
};
2627
```
27-
## A note on static assets
28-
Precompiled pages, client and static resources should be served independently of your dynamic content. One solution to this could be to upload the `build/assets/` directory to S3 and using its static site hosting functionality. Then, by using a CDN like CloudFront, requests could be routed to the correct origin.
28+
Copy `serverless.yml` from the root of this repo to the root of your project
29+
30+
After building your app run `sls deploy` to deploy code to AWS.
31+
32+
Your app can then be accessed via the CloudFront distribution created as aprt of the stack.
33+
34+
## Static Assets and precompiled pages
35+
To server static assets and precompiled pages this adapter makes use of S3. In order to route traffic the correct destination a Lambda@edge fuction is used to perfrom a origin rewrite is used to redirect traffic to the S3 Bucket.

index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { copyFileSync, unlinkSync, existsSync, mkdirSync } = require('fs');
1+
const { copyFileSync, unlinkSync, existsSync, mkdirSync, statSync, readdirSync, writeFileSync } = require('fs');
22
const { join } = require('path');
33

44
const esbuild = require('esbuild');
@@ -24,6 +24,11 @@ module.exports = function ({ out = 'build' } = {}) {
2424
mkdirSync(server_directory, { recursive: true });
2525
}
2626

27+
const edge_directory = join(out, 'edge');
28+
if (!existsSync(edge_directory)) {
29+
mkdirSync(edge_directory, { recursive: true });
30+
}
31+
2732
builder.log.minor('Copying assets');
2833
builder.writeClient(static_directory);
2934
builder.writeStatic(static_directory);
@@ -51,11 +56,42 @@ module.exports = function ({ out = 'build' } = {}) {
5156
dest: `${static_directory}`,
5257
});
5358

59+
console.log('Building router');
60+
copyFileSync(`${__dirname}/files/router.js`, `${edge_directory}/_router.js`);
61+
writeFileSync(`${edge_directory}/static.js`, `export default ${JSON.stringify(getAllFiles(static_directory))}`)
62+
63+
esbuild.buildSync({
64+
entryPoints: [`${edge_directory}/_router.js`],
65+
outfile: `${edge_directory}/router.js`,
66+
format: 'cjs',
67+
bundle: true,
68+
platform: 'node',
69+
});
70+
71+
5472
builder.log.minor('Cleanup');
5573
unlinkSync(`${server_directory}/_serverless.js`);
74+
unlinkSync(`${edge_directory}/_router.js`);
5675
unlinkSync(`${out}/app.js`);
5776
},
5877
};
5978

6079
return adapter;
6180
};
81+
82+
const getAllFiles = function (dirPath, basePath, arrayOfFiles) {
83+
files = readdirSync(dirPath)
84+
85+
arrayOfFiles = arrayOfFiles || []
86+
basePath = basePath || dirPath
87+
88+
files.forEach(function (file) {
89+
if (statSync(dirPath + "/" + file).isDirectory()) {
90+
arrayOfFiles = getAllFiles(dirPath + "/" + file, basePath, arrayOfFiles)
91+
} else {
92+
arrayOfFiles.push(join("/", dirPath.replace(basePath, ''), "/", file))
93+
}
94+
})
95+
96+
return arrayOfFiles
97+
}

0 commit comments

Comments
 (0)