examples: use application 404 page

This commit is contained in:
Greg Johnston 2024-06-12 08:07:27 -04:00
parent 48028b476a
commit d133cff092
3 changed files with 25 additions and 30 deletions

View File

@ -1,5 +1,6 @@
use lazy_static::lazy_static;
use leptos::prelude::*;
use leptos_meta::MetaTags;
use leptos_meta::*;
use leptos_router::{
components::{FlatRoutes, Route, Router},
@ -10,6 +11,24 @@ use leptos_router::{
use serde::{Deserialize, Serialize};
use thiserror::Error;
pub fn server_app(leptos_options: &LeptosOptions) -> impl IntoView {
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<AutoReload options=leptos_options.clone() />
<HydrationScripts options=leptos_options.clone()/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}
#[component]
pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc.

View File

@ -1,11 +1,11 @@
use crate::app::App;
use crate::app::server_app;
use axum::{
body::Body,
extract::State,
http::{Request, Response, StatusCode, Uri},
response::{IntoResponse, Response as AxumResponse},
};
use leptos::{config::LeptosOptions, view};
use leptos::config::LeptosOptions;
use tower::ServiceExt;
use tower_http::services::ServeDir;
@ -20,7 +20,8 @@ pub async fn file_and_error_handler(
if res.status() == StatusCode::OK {
res.into_response()
} else {
let handler = leptos_axum::render_app_to_stream(move || "404");
let handler =
leptos_axum::render_app_to_stream(move || server_app(&options));
handler(req).await.into_response()
}
}

View File

@ -2,10 +2,8 @@
#[tokio::main]
async fn main() {
use axum::Router;
use leptos::logging;
use leptos::prelude::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
use leptos_meta::MetaTags;
use ssr_modes_axum::{app::*, fallback::file_and_error_handler};
let conf = get_configuration(None).await.unwrap();
@ -14,34 +12,11 @@ async fn main() {
// Generate the list of routes in your Leptos App
let routes = generate_route_list(App);
// Explicit server function registration is no longer required
// on the main branch. On 0.3.0 and earlier, uncomment the lines
// below to register the server functions.
// _ = GetPost::register();
// _ = ListPostMetadata::register();
let app = Router::new()
.leptos_routes(&leptos_options, routes, {
let leptos_options = leptos_options.clone();
move || {
use leptos::prelude::*;
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
// <AutoReload options=app_state.leptos_options.clone() />
<HydrationScripts options=leptos_options.clone()/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}})
move || server_app(&leptos_options)
})
.fallback(file_and_error_handler)
.with_state(leptos_options);