Skip to content

Commit

Permalink
feat(plugins): added functional actions for exporting error pages
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jan 16, 2022
1 parent 9ee6904 commit 36abcc1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/next/en-US/plugins/functional.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ If you'd like to request that a new action, functional or control, be added, ple
- `after_failed_static_alias_dir_copy` -- runs arbitrary code if the export process fails to copy a static alias that was a directory (e.g. to report the failed export to a server crash management system)
- `after_failed_static_alias_file_copy` -- runs arbitrary code if the export process fails to copy a static alias that was a file (e.g. to report the failed export to a server crash management system)
- `after_successful_export` -- runs arbitrary code after the export process has completed, if it was successful (e.g. copying custom files into `.perseus/dist/`)
- `export_error_page_actions` --- actions that'll be run when exporting an error page
- `before_export_error_page` --- runs arbitrary code before this process has started (providing the error code to be exported for and the output file)
- `after_successful_export_error_page` -- runs arbitrary code after this process has completed, if it was successful
- `after_failed_write` -- runs arbitrary code after this process has completed, if it couldn't write to the target output file
- `server_actions` -- actions that'll be run as part of the Perseus server when the user runs `perseus serve` (or when a [serverful production deployment](:deploying/serverful) runs)
- `before_serve` -- runs arbitrary code before the server starts (e.g. to spawn an API server)
- `client_actions` -- actions that'll run in the browser when the user's app is accessed
Expand Down
20 changes: 17 additions & 3 deletions examples/basic/.perseus/builder/src/bin/export_error_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async fn real_main() -> i32 {
env::set_current_dir("../").unwrap();

let plugins = get_plugins::<SsrNode>();

let error_pages = get_error_pages(&plugins);
let root_id = get_app_root(&plugins);
let immutable_store = get_immutable_store(&plugins);
Expand Down Expand Up @@ -62,6 +63,14 @@ async fn real_main() -> i32 {
return 1;
}
};
plugins
.functional_actions
.export_error_page_actions
.before_export_error_page
.run(
(err_code_to_build_for, output.to_string()),
plugins.get_plugin_data(),
);
// Build that error page as the server does
let err_page_str = build_error_page(
"",
Expand All @@ -76,18 +85,23 @@ async fn real_main() -> i32 {
// Write that to the mandatory second argument (the output location)
// We'll move out of `.perseus/` first though
env::set_current_dir("../").unwrap();
match fs::write(output, err_page_str) {
match fs::write(&output, err_page_str) {
Ok(_) => (),
Err(err) => {
eprintln!("{}", fmt_err(&err));
plugins
.functional_actions
.export_error_page_actions
.after_failed_write
.run((err, output.to_string()), plugins.get_plugin_data());
return 1;
}
};

plugins
.functional_actions
.export_actions
.after_successful_export
.export_error_page_actions
.after_successful_export_error_page
.run((), plugins.get_plugin_data());
println!("Static exporting successfully completed!");
0
Expand Down
13 changes: 13 additions & 0 deletions packages/perseus/src/plugins/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub struct FunctionalPluginActions<G: Html> {
pub build_actions: FunctionalPluginBuildActions,
/// Actions pertaining to the export process.
pub export_actions: FunctionalPluginExportActions,
/// Actions pertaining to the process of exporting an error page.
pub export_error_page_actions: FunctionalPluginExportErrorPageActions,
/// Actions pertaining to the server.
pub server_actions: FunctionalPluginServerActions,
/// Actions pertaining to the client-side code.
Expand All @@ -76,6 +78,7 @@ impl<G: Html> Default for FunctionalPluginActions<G> {
settings_actions: FunctionalPluginSettingsActions::<G>::default(),
build_actions: FunctionalPluginBuildActions::default(),
export_actions: FunctionalPluginExportActions::default(),
export_error_page_actions: FunctionalPluginExportErrorPageActions::default(),
server_actions: FunctionalPluginServerActions::default(),
client_actions: FunctionalPluginClientActions::default(),
}
Expand Down Expand Up @@ -139,6 +142,16 @@ pub struct FunctionalPluginExportActions {
/// Runs after the export process if it completes successfully.
pub after_successful_export: FunctionalPluginAction<(), ()>,
}
/// Functional actions that pertain to the process of exporting an error page.
#[derive(Default)]
pub struct FunctionalPluginExportErrorPageActions {
/// Runs before the process of exporting an error page, providing the HTTP status code to be exported and the output filename (relative to the root of the project, not to `.perseus/`).
pub before_export_error_page: FunctionalPluginAction<(u16, String), ()>,
/// Runs after a error page was exported successfully.
pub after_successful_export_error_page: FunctionalPluginAction<(), ()>,
/// Runs if writing to the output file failed. Error and filename are given.
pub after_failed_write: FunctionalPluginAction<(std::io::Error, String), ()>,
}
/// Functional actions that pertain to the server.
#[derive(Default)]
pub struct FunctionalPluginServerActions {
Expand Down

0 comments on commit 36abcc1

Please sign in to comment.