UNPKG

@serverless-stack/nextjs-lambda

Version:

Provides handlers that can be used in CloudFront Lambda@Edge to deploy next.js applications to the edge

72 lines (71 loc) 2.77 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.s3StorePage = void 0; const s3StorePage = async (options) => { const { S3Client } = await Promise.resolve().then(() => __importStar(require("@aws-sdk/client-s3/S3Client"))); const s3 = new S3Client({ region: options.region, maxAttempts: 3 }); const s3BasePath = options.basePath ? `${options.basePath.replace(/^\//, "")}/` : ""; const baseKey = options.uri .replace(/^\/$/, "index") .replace(/^\//, "") .replace(/\.(json|html)$/, "") .replace(/^_next\/data\/[^\/]*\//, ""); const jsonKey = `_next/data/${options.buildId}/${baseKey}.json`; const htmlKey = `static-pages/${options.buildId}/${baseKey}.html`; const cacheControl = options.revalidate ? undefined : "public, max-age=0, s-maxage=2678400, must-revalidate"; const expires = options.revalidate ? new Date(new Date().getTime() + 1000 * options.revalidate) : undefined; const s3JsonParams = { Bucket: options.bucketName, Key: `${s3BasePath}${jsonKey}`, Body: JSON.stringify(options.pageData), ContentType: "application/json", CacheControl: cacheControl, Expires: expires }; const s3HtmlParams = { Bucket: options.bucketName, Key: `${s3BasePath}${htmlKey}`, Body: options.html, ContentType: "text/html", CacheControl: cacheControl, Expires: expires }; const { PutObjectCommand } = await Promise.resolve().then(() => __importStar(require("@aws-sdk/client-s3/commands/PutObjectCommand"))); await Promise.all([ s3.send(new PutObjectCommand(s3JsonParams)), s3.send(new PutObjectCommand(s3HtmlParams)) ]); return { cacheControl, expires }; }; exports.s3StorePage = s3StorePage;