Skip to content

Commit

Permalink
Move to lambda at edge based routing
Browse files Browse the repository at this point in the history
  • Loading branch information
James Bray committed Jan 24, 2022
1 parent 2d86ac9 commit e97d5d5
Show file tree
Hide file tree
Showing 7 changed files with 14,468 additions and 12 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# adapter-lambda for SvelteKit

An adapter to build a [SvelteKit](https://kit.svelte.dev/) app into a lambda ready for deployment with lambda proxy.

## Installation
```
npm install --save-dev @yarbsemaj/adapter-lambda
```

## Usage

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

};
```
## A note on static assets
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.
Copy `serverless.yml` from the root of this repo to the root of your project

After building your app run `sls deploy` to deploy code to AWS.

Your app can then be accessed via the CloudFront distribution created as aprt of the stack.

## Static Assets and precompiled pages
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.
38 changes: 37 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { copyFileSync, unlinkSync, existsSync, mkdirSync } = require('fs');
const { copyFileSync, unlinkSync, existsSync, mkdirSync, statSync, readdirSync, writeFileSync } = require('fs');
const { join } = require('path');

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

const edge_directory = join(out, 'edge');
if (!existsSync(edge_directory)) {
mkdirSync(edge_directory, { recursive: true });
}

builder.log.minor('Copying assets');
builder.writeClient(static_directory);
builder.writeStatic(static_directory);
Expand Down Expand Up @@ -51,11 +56,42 @@ module.exports = function ({ out = 'build' } = {}) {
dest: `${static_directory}`,
});

console.log('Building router');
copyFileSync(`${__dirname}/files/router.js`, `${edge_directory}/_router.js`);
writeFileSync(`${edge_directory}/static.js`, `export default ${JSON.stringify(getAllFiles(static_directory))}`)

esbuild.buildSync({
entryPoints: [`${edge_directory}/_router.js`],
outfile: `${edge_directory}/router.js`,
format: 'cjs',
bundle: true,
platform: 'node',
});


builder.log.minor('Cleanup');
unlinkSync(`${server_directory}/_serverless.js`);
unlinkSync(`${edge_directory}/_router.js`);
unlinkSync(`${out}/app.js`);
},
};

return adapter;
};

const getAllFiles = function (dirPath, basePath, arrayOfFiles) {
files = readdirSync(dirPath)

arrayOfFiles = arrayOfFiles || []
basePath = basePath || dirPath

files.forEach(function (file) {
if (statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, basePath, arrayOfFiles)
} else {
arrayOfFiles.push(join("/", dirPath.replace(basePath, ''), "/", file))
}
})

return arrayOfFiles
}
Loading

0 comments on commit e97d5d5

Please sign in to comment.