···11+---
22+cargo/maudit: minor
33+---
44+55+Adds a new `redirect()` function that can used to generate redirects to other pages and websites. This function is exported from the route prelude.
66+77+```rs
88+use maudit::route::prelude::*;
99+1010+#[route("/redirect")]
1111+pub struct Redirect;
1212+1313+impl Route for Redirect {
1414+ fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> {
1515+ redirect("https://example.com")
1616+1717+ // Use a page's url method to generate type safe links:
1818+ // redirect(&OtherPage.url(None))
1919+ }
2020+}
2121+```
+49-1
crates/maudit/src/route.rs
···230230 routes
231231}
232232233233+/// Generates the required HTML for a redirect to the specified URL.
234234+///
235235+/// This function returns a RenderResult and as such can be used directly as a possible return value inside a page
236236+///
237237+/// ## Example
238238+/// ```rust
239239+/// use maudit::route::prelude::*;
240240+///
241241+/// # #[route("/other_page")]
242242+/// # pub struct OtherPage;
243243+/// # impl Route for OtherPage {
244244+/// # fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> {""}
245245+/// # }
246246+///
247247+/// #[route("/redirect")]
248248+/// pub struct Redirect;
249249+///
250250+/// impl Route for Redirect {
251251+/// fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> {
252252+/// redirect("https://example.com")
253253+///
254254+/// // Use a page's url method to generate type safe links:
255255+/// // redirect(&OtherPage.url(None))
256256+/// }
257257+/// }
258258+/// ```
259259+pub fn redirect(url: &str) -> RenderResult {
260260+ RenderResult::Text(format!(
261261+ r#"<meta http-equiv="refresh" content="0; url={}" />"#,
262262+ url
263263+ ))
264264+}
265265+233266/// Allows to access various data and assets in a [`Route`] implementation.
234267///
235268/// ## Example
···926959 //! ```
927960 pub use super::{
928961 CachedRoute, DynamicRouteContext, FullRoute, Page, PageContext, PageParams, Pages,
929929- PaginatedContentPage, PaginationPage, RenderResult, Route, RouteExt, paginate,
962962+ PaginatedContentPage, PaginationPage, RenderResult, Route, RouteExt, paginate, redirect,
930963 };
931964 pub use crate::assets::{
932965 Asset, Image, ImageFormat, ImageOptions, ImagePlaceholder, RenderWithAlt, Script, Style,
···13011334 let expected = Path::new("/dist/articles/hello-world/index.html");
1302133513031336 assert_eq!(page.file_path(&route_params, output_dir), expected);
13371337+ }
13381338+13391339+ #[test]
13401340+ fn test_redirect_simple_url() {
13411341+ let result = redirect("https://example.com");
13421342+13431343+ match result {
13441344+ RenderResult::Text(html) => {
13451345+ assert_eq!(
13461346+ html,
13471347+ r#"<meta http-equiv="refresh" content="0; url=https://example.com" />"#
13481348+ );
13491349+ }
13501350+ _ => panic!("Expected RenderResult::Text variant"),
13511351+ }
13041352 }
13051353}