Improve browser caching with cache control header

Not only does the header need to be added, but the signed URL needs to
have it's expiry rounded so that the return url is the same for a given
window, say 10minutes

I followed this https://advancedweb.hu/cacheable-s3-signed-urls/
basically what this means is that because we're caching the assets
themselves, if as user asks for a part that already exists we'll return
a url for the existing part instead of regenerating it, however if it
was them that generated the part less than 10 minutes ago, they'll still
have to download the asset again. This way it will save us costs and
will be quicker for them.

Resolves #334
This commit is contained in:
Kurt Hutten
2021-05-31 16:56:21 +10:00
parent c049a1d3db
commit 913045fe12
8 changed files with 626 additions and 2759 deletions

View File

@@ -2,6 +2,7 @@ const { runScad, stlExport } = require('./runScad')
const middy = require('middy')
const { cors } = require('middy/middlewares')
const AWS = require('aws-sdk')
const tk = require('timekeeper')
const {
makeHash,
checkIfAlreadyExists,
@@ -53,7 +54,8 @@ const preview = async (req, _context, callback) => {
Bucket: process.env.BUCKET,
Key: previousAssetStl.isAlreadyInBucket ? stlKey : key,
},
s3
s3,
tk
),
consoleMessage:
previousAsset.consoleMessage || previousAssetPng.consoleMessage,
@@ -74,6 +76,7 @@ const preview = async (req, _context, callback) => {
key,
s3,
params,
tk,
})
}
@@ -96,7 +99,7 @@ const stl = async (req, _context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
url: getObjectUrl({ ...params }, s3),
url: getObjectUrl({ ...params }, s3, tk),
consoleMessage: previousAsset.consoleMessage,
}),
}
@@ -113,6 +116,7 @@ const stl = async (req, _context, callback) => {
key: stlKey,
s3,
params,
tk,
})
}

File diff suppressed because it is too large Load Diff

View File

@@ -9,7 +9,8 @@
"aws-sdk": "^2.907.0",
"cors": "^2.8.5",
"middy": "^0.36.0",
"nanoid": "^3.1.20"
"nanoid": "^3.1.20",
"timekeeper": "2.2.0"
},
"devDependencies": {
"aws-lambda-ric": "^1.0.0"