Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix search index generation #406

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ jobs:
- name: Install CLI
run: cargo binstall dioxus-cli -y --force --version 0.6
- name: Build
run: DIOXUS_LOG=dx=trace dx build --platform web --fullstack --features fullstack --release --ssg
run: DIOXUS_LOG=dx=trace dx build --platform web --fullstack --features fullstack,production --release --ssg
- name: Generate search index
run: target/dx/dioxus_docs_site/release/web/server --generate-search-index
- name: Copy output
run: cp -r target/dx/dioxus_docs_site/release/web/public docs
- name: Add gh pages 404
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,4 @@ doc_test = [
"server",
"dioxus/fullstack"
]
production = []
4 changes: 2 additions & 2 deletions src/components/nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ fn SearchModal() -> Element {
let mut search_text = use_signal(String::new);

let search_index = use_resource(|| async move {
#[cfg(debug_assertions)]
#[cfg(not(feature = "production"))]
let url = "http://localhost:8080/assets/dioxus_search/index_searchable.bin";

#[cfg(not(debug_assertions))]
#[cfg(feature = "production")]
let url = "https://dioxuslabs.com/assets/dioxus_search/index_searchable.bin";

let data = reqwest::get(url).await.ok()?.bytes().await.ok()?;
Expand Down
27 changes: 7 additions & 20 deletions src/components/search.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
use dioxus::prelude::*;

/// we use a component at the end of the router to generate the search index once the rest
/// of the pages have been ssr-ed during SSG.
#[component]
pub fn Search() -> Element {
pub fn generate_search_index() {
#[cfg(not(target_arch = "wasm32"))]
{
use crate::{static_dir, Route};
use once_cell::sync::Lazy;

static _INDEX: Lazy<bool> = Lazy::new(|| {
std::env::set_var("CARGO_MANIFEST_DIR", static_dir().join("assets"));
dioxus_search::SearchIndex::<Route>::create(
"searchable",
dioxus_search::BaseDirectoryMapping::new(static_dir()),
);
use std::sync::atomic::{AtomicBool, Ordering};

true
});

assert!(*_INDEX == true);
std::env::set_var("CARGO_MANIFEST_DIR", static_dir().join("assets"));
dioxus_search::SearchIndex::<Route>::create(
"searchable",
dioxus_search::BaseDirectoryMapping::new(static_dir()),
);
}

rsx! {}
}
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ pub mod snippets;
pub use components::*;

fn main() {
// If we are just building the search index, we don't need to launch the app
#[cfg(feature = "server")]
if std::env::args().any(|arg| arg == "--generate-search-index") {
search::generate_search_index();
return;
}

create_sitemap();

dioxus::LaunchBuilder::new()
Expand Down Expand Up @@ -187,11 +194,6 @@ pub enum Route {
#[end_layout]
#[end_nest]

// once all the routes above have been generated, we build the search index
// a bit of a hack, sorry
#[route("/search")]
Search {},

#[redirect("/docs/:..segments", |segments: Vec<String>| {
let joined = segments.join("/");
let docs_route = format!("/{}", joined.trim_matches('/'));
Expand Down