Skip to content

Commit

Permalink
Basic support for static paths. Does not cache new routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
oseibonsu committed Mar 17, 2020
1 parent 2e7e234 commit 6fcfa60
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
12 changes: 9 additions & 3 deletions packages/serverless-nextjs-component/default-lambda-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ exports.handler = async event => {

const isStaticPage = pages.html.nonDynamic[uri];
const isPublicFile = publicFiles[uri];
const isStaticPath = pages.staticPath[uri];
const isDataPath = uri.startsWith("_next/data/");

if (isStaticPage || isPublicFile) {
request.origin.s3.path = isStaticPage ? "/static-pages" : "/public";
if (isDataPath) {
return request;
}

if (isStaticPage || isPublicFile || isStaticPath) {
request.origin.s3.path = isStaticPage || isStaticPath ? "/static-pages" : "/public";

if (isStaticPage) {
if (isStaticPage || isStaticPath) {
request.uri = uri + ".html";
}

Expand Down
49 changes: 48 additions & 1 deletion packages/serverless-nextjs-component/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ class NextjsComponent extends Component {
return sortedPagesManifest;
}

async readPrerenderManifest(nextConfigPath) {
const prerenderManifestPath = join(nextConfigPath, ".next/prerender-manifest.json");
const hasPrerenderManifest = await fse.exists(prerenderManifestPath);
if (!hasPrerenderManifest) {
return Promise.reject(
"prerender-manifest not found."
);
}

const prerenderManifest = await fse.readJSON(prerenderManifestPath);

const staticPathRoutes = Object.keys(prerenderManifest.routes)
.map(route => ({
[route]: {
html: `pages${route}.html`,
json: {
file: `pages${route}.json`,
path: prerenderManifest.routes[route].dataRoute.slice(1),
}
}
}))
.reduce(function (obj, item) {
const key = Object.keys(item)[0]
obj[key] = item[key];
return obj;
}, {});

return staticPathRoutes;
}

readDefaultBuildManifest(nextConfigPath) {
return fse.readJSON(
join(nextConfigPath, ".serverless_nextjs/default-lambda/manifest.json")
Expand All @@ -105,9 +135,11 @@ class NextjsComponent extends Component {

async prepareBuildManifests(nextConfigPath) {
const pagesManifest = await this.readPagesManifest(nextConfigPath);
const staticPathRoutes = await this.readPrerenderManifest(nextConfigPath);

const defaultBuildManifest = {
pages: {
staticPath: staticPathRoutes,
ssr: {
dynamic: {},
nonDynamic: {}
Expand Down Expand Up @@ -312,14 +344,29 @@ class NextjsComponent extends Component {
defaultBuildManifest.pages.html.dynamic
).map(x => x.file);

const uploadHtmlPages = [...nonDynamicHtmlPages, ...dynamicHtmlPages].map(
const staticPathPages = Object.values(
defaultBuildManifest.pages.staticPath
).map(x => x.html);

const staticPathJson = Object.values(
defaultBuildManifest.pages.staticPath
).map(x => x.json);

const uploadHtmlPages = [...nonDynamicHtmlPages, ...dynamicHtmlPages, ...staticPathPages].map(
page =>
bucket.upload({
file: join(nextConfigPath, ".next/serverless", page),
key: `static-pages/${page.replace("pages/", "")}`
})
);

const uploadJson = staticPathJson.map(
json => bucket.upload({
file: join(nextConfigPath, ".next/serverless", json.file),
key: json.path
})
);

const assetsUpload = [
bucket.upload({
dir: join(nextConfigPath, ".next/static"),
Expand Down

0 comments on commit 6fcfa60

Please sign in to comment.