Skip to main content

is_ssr_fetch

Function is_ssr_fetch 

Source
pub fn is_ssr_fetch(req: &HttpRequest) -> bool
Expand description

Whether the request is a fetch the app made while the server rendered a page, sent back to the server’s own origin (MountConfigBuilder::ssr_fetch_base).

A handler of your own that renders pages with ServerState should refuse such a request, as vertigo_handler does: rendering a page for it could fetch the same URL again, endlessly. It comes when the app fetches a URL that only the page handler answers.

use actix_web::{HttpRequest, HttpResponse};
use vertigo_cli::serve::{RequestHeaders, ServerState, is_ssr_fetch, request_cookie};

async fn page(req: HttpRequest) -> HttpResponse {
    if is_ssr_fetch(&req) {
        return HttpResponse::NotFound().finish();
    }
    let cookie = request_cookie(&req);
    let headers = RequestHeaders {
        cookie: cookie.as_deref(),
        ..RequestHeaders::default()
    };
    ServerState::global("/")
        .request_with_headers(&req.uri().to_string(), headers)
        .await
        .into()
}