Skip to content

Commit

Permalink
feat: divide prerendering-related plugins into separate files
Browse files Browse the repository at this point in the history
This will enable developers to pick which plugins they need. Also, this makes it much easier to pick all except one, which needs to be modified by the dev.
  • Loading branch information
adrians5j committed Oct 12, 2021
1 parent 7fcb187 commit 688ce46
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 177 deletions.
175 changes: 0 additions & 175 deletions packages/api-page-builder/src/prerendering/hooks.ts

This file was deleted.

@@ -0,0 +1,13 @@
import { MenuPlugin } from "~/plugins/MenuPlugin";

export default () => [
new MenuPlugin({
// After a menu has changed, invalidate all pages that contain the updated menu.
async afterUpdate({ context, menu }) {
await context.pageBuilder.pages.prerendering.render({
context,
tags: [{ tag: { key: "pb-menu", value: menu.slug } }]
});
}
})
];
@@ -0,0 +1,32 @@
import { PagePlugin } from "~/plugins/PagePlugin";

export default () => [
new PagePlugin({
// After we deleted a page, we need to clear prerender files / cache as well, if the page was published.
async afterDelete({ context, page, publishedPage }) {
// Published pages have this record.
if (!publishedPage) {
return;
}

if (page.version === 1) {
return context.pageBuilder.pages.prerendering.flush({
context,
paths: [{ path: publishedPage.path }]
});
}

// If the published version was deleted.
const isPublished = publishedPage.id === page.id;
if (isPublished) {
return context.pageBuilder.pages.prerendering.flush({
context,
paths: [{ path: publishedPage.path }]
});
}

// Note: special pages (404 / home) cannot be deleted, that's why
// there is no special handling in regards to that here.
}
})
];
@@ -0,0 +1,67 @@
import { PagePlugin } from "~/plugins/PagePlugin";
import lodashGet from "lodash/get";

const NOT_FOUND_FOLDER = "_NOT_FOUND_PAGE_";

export default () => [
new PagePlugin({
// After a page was published, we need to render the page.
async afterPublish({ context, page, publishedPage }) {
const promises = [];
promises.push(
context.pageBuilder.pages.prerendering.render({
context,
paths: [{ path: page.path }]
})
);

const settings = await context.pageBuilder.settings.getCurrent();

const homePage = lodashGet(settings, "pages.home");
// If we just published a page that is set as current homepage, let's rerender the "/" path as well.
if (homePage === page.pid) {
promises.push(
context.pageBuilder.pages.prerendering.render({
context,
paths: [{ path: "/" }]
})
);
}

const notFoundPage = lodashGet(settings, "pages.notFound");
// Finally, if we just published a page that is set as current not-found page, let's do
// another rerender and save that into the NOT_FOUND_FOLDER.
if (notFoundPage === page.pid) {
promises.push(
context.pageBuilder.pages.prerendering.render({
context,
paths: [
{
path: page.path,
configuration: {
meta: {
notFoundPage: true
},
storage: { folder: NOT_FOUND_FOLDER }
}
}
]
})
);
}

// If we had a published page and the URL on which it was published is different than
// the URL of the just published page, then let's flush the page on old URL.
if (publishedPage && publishedPage.path !== page.path) {
promises.push(
context.pageBuilder.pages.prerendering.flush({
context,
paths: [{ path: publishedPage.path }]
})
);
}

await Promise.all(promises);
}
})
];
@@ -0,0 +1,20 @@
import { PagePlugin } from "~/plugins/PagePlugin";

export default () => [
new PagePlugin({
// After a page was unpublished, we need to flush the page.
async afterUnpublish({ context, page }) {
const promises = [];
promises.push(
context.pageBuilder.pages.prerendering.flush({
context,
paths: [{ path: page.path }]
})
);

// Note: special pages (404 / home) cannot be unpublished, that's why
// there is no special handling in regards to that here.
await Promise.all(promises);
}
})
];
@@ -0,0 +1,47 @@
import { SettingsPlugin } from "~/plugins/SettingsPlugin";

const NOT_FOUND_FOLDER = "_NOT_FOUND_PAGE_";

export default () => [
new SettingsPlugin({
// After settings were changed, invalidate all pages that contain pb-page tag.
async afterUpdate({ context, nextSettings, meta }) {
if (!nextSettings) {
return;
}

// TODO: optimize this.
// TODO: right now, on each update of settings, we trigger a complete site rebuild.
await context.pageBuilder.pages.prerendering.render({
context,
tags: [{ tag: { key: "pb-page" } }]
});

// If a change on pages settings (home, notFound) has been made, let's rerender accordingly.
for (let i = 0; i < meta.diff.pages.length; i++) {
const [type, , , page] = meta.diff.pages[i];
switch (type) {
case "home":
await context.pageBuilder.pages.prerendering.render({
context,
paths: [{ path: "/" }]
});
break;
case "notFound":
await context.pageBuilder.pages.prerendering.render({
context,
paths: [
{
path: page.path,
configuration: {
storage: { folder: NOT_FOUND_FOLDER }
}
}
]
});
break;
}
}
}
})
];
15 changes: 15 additions & 0 deletions packages/api-page-builder/src/prerendering/hooks/index.ts
@@ -0,0 +1,15 @@
import afterMenuUpdate from "./afterMenuUpdate";
import afterPageDelete from "./afterPageDelete";
import afterPagePublish from "./afterPagePublish";
import afterPageUnpublish from "./afterPageUnpublish";
import afterSettingsUpdate from "./afterSettingsUpdate";

export default () => {
return [
afterMenuUpdate(),
afterPageDelete(),
afterPagePublish(),
afterPageUnpublish(),
afterSettingsUpdate()
];
};
4 changes: 2 additions & 2 deletions packages/api-page-builder/src/prerendering/index.ts
@@ -1,6 +1,6 @@
import { prerenderingHandlers } from "./prerenderingHandlers";
import prerenderingHookPlugins from "./hooks";
import hooks from "./hooks";

export default () => {
return [prerenderingHandlers, ...prerenderingHookPlugins()];
return [prerenderingHandlers, ...hooks()];
};

0 comments on commit 688ce46

Please sign in to comment.