Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: divide prerendering-related plugins into separate files
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
Showing
8 changed files
with
196 additions
and
177 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
packages/api-page-builder/src/prerendering/hooks/afterMenuUpdate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } }] | ||
}); | ||
} | ||
}) | ||
]; |
32 changes: 32 additions & 0 deletions
32
packages/api-page-builder/src/prerendering/hooks/afterPageDelete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} | ||
}) | ||
]; |
67 changes: 67 additions & 0 deletions
67
packages/api-page-builder/src/prerendering/hooks/afterPagePublish.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}) | ||
]; |
20 changes: 20 additions & 0 deletions
20
packages/api-page-builder/src/prerendering/hooks/afterPageUnpublish.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}) | ||
]; |
47 changes: 47 additions & 0 deletions
47
packages/api-page-builder/src/prerendering/hooks/afterSettingsUpdate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
}) | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { prerenderingHandlers } from "./prerenderingHandlers"; | ||
import prerenderingHookPlugins from "./hooks"; | ||
import hooks from "./hooks"; | ||
|
||
export default () => { | ||
return [prerenderingHandlers, ...prerenderingHookPlugins()]; | ||
return [prerenderingHandlers, ...hooks()]; | ||
}; |