Tes: wait Seite speichern in indexedDB vor dem Aufruf
This commit is contained in:
42
app/page.js
42
app/page.js
@@ -1,31 +1,33 @@
|
||||
"use client";
|
||||
import { useEffect } from "react";
|
||||
import { useRouter } from "next/navigation"; // App-Router Hook für Navigation
|
||||
import { storePage } from "../utils/indexedDB"; // Importiere die Funktion storePage aus indexedDB.js
|
||||
|
||||
// Importiere `storePage` nur im Client
|
||||
export default function Home() {
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window !== "undefined" && storePage) {
|
||||
// Speichere die Seite "wait" als HTML in IndexedDB
|
||||
const pageContent = `
|
||||
<div style="text-align: center; margin-top: 100px;">
|
||||
<h1>Bitte warten...</h1>
|
||||
<div class="spinner"></div>
|
||||
<p>Die Seite wird automatisch neu geladen.</p>
|
||||
</div>`;
|
||||
storePage("waitPage", new Blob([pageContent], { type: "text/html" }))
|
||||
.then(() => {
|
||||
console.log("Seite 'wait' erfolgreich gespeichert.");
|
||||
// Weiterleitung zur Dashboard-Seite
|
||||
router.push("/dashboard");
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Fehler beim Speichern der Seite 'wait':", error);
|
||||
// Auch bei einem Fehler weiter zur Dashboard-Seite leiten
|
||||
router.push("/dashboard");
|
||||
});
|
||||
// Überprüfe, ob der Code im Browser läuft
|
||||
if (typeof window !== "undefined") {
|
||||
// Dynamischer Import von `storePage` nur im Browser
|
||||
import("../utils/indexedDB").then(({ storePage }) => {
|
||||
const pageContent = `
|
||||
<div style="text-align: center; margin-top: 100px;">
|
||||
<h1>Bitte warten...</h1>
|
||||
<div class="spinner"></div>
|
||||
<p>Die Seite wird automatisch neu geladen.</p>
|
||||
</div>`;
|
||||
storePage("waitPage", new Blob([pageContent], { type: "text/html" }))
|
||||
.then(() => {
|
||||
console.log("Seite 'wait' erfolgreich gespeichert.");
|
||||
// Weiterleitung zur Dashboard-Seite
|
||||
router.push("/dashboard");
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Fehler beim Speichern der Seite 'wait':", error);
|
||||
router.push("/dashboard");
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// Falls kein Browser oder IndexedDB verfügbar ist, direkt zur Dashboard-Seite weiterleiten
|
||||
router.push("/dashboard");
|
||||
|
||||
@@ -2,11 +2,13 @@ import { openDB } from "idb"; // utils/indexedDB.js
|
||||
|
||||
const dbPromise = openDB("my-pdf-store", 1, {
|
||||
upgrade(db) {
|
||||
// Überprüfe und erstelle den Object Store für PDFs
|
||||
if (!db.objectStoreNames.contains("pdfs")) {
|
||||
db.createObjectStore("pdfs");
|
||||
}
|
||||
// Überprüfe und erstelle den Object Store für Seiten (pages)
|
||||
if (!db.objectStoreNames.contains("pages")) {
|
||||
db.createObjectStore("pages");
|
||||
db.createObjectStore("pages"); // Korrekte Erstellung des "pages" Object Stores
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -25,7 +27,11 @@ export async function getPDF(name) {
|
||||
// Store page
|
||||
export async function storePage(name, file) {
|
||||
const db = await dbPromise;
|
||||
await db.put("pages", file, name);
|
||||
// Überprüfe, ob der Object Store "pages" existiert
|
||||
const transaction = db.transaction("pages", "readwrite");
|
||||
const store = transaction.objectStore("pages");
|
||||
await store.put(file, name);
|
||||
await transaction.done;
|
||||
}
|
||||
|
||||
export async function getPage(name) {
|
||||
|
||||
Reference in New Issue
Block a user