Skip to content

Commit

Permalink
release 0.1.0 fix (#42)
Browse files Browse the repository at this point in the history
* fix: indexDB initialization (#41)
  • Loading branch information
SchneiderNicolas authored Oct 16, 2023
1 parent 25a2793 commit 105749e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions front/src/config/config.prod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const config = {
API_BASE_URL: 'https://api.pwa.nicolas-schneider.fr',
BASE_URL: 'https://pwa.nicolas-schneider.fr',
API_BASE_URL: 'http://localhost:8080',
BASE_URL: 'http://localhost:3000/',
PUBLIC_VAPID_KEY:
'BDwN10CsEWgEYtF1fGEc_Y7iXOPseiYp-N9-l2KyNmp6RV4F7M7nVPBeuVxHvV63T4PXXfh-XKTRq4FuBcKajNQ',
} as const;
Expand Down
5 changes: 4 additions & 1 deletion front/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import './index.css';
import App from './App';
import * as serviceWorkerRegistration from './serviceWorkerRegistration';
import reportWebVitals from './reportWebVitals';
import AppInitializer from './utils/AppInitializer';

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement,
);
root.render(
<React.StrictMode>
<App />
<AppInitializer>
<App />
</AppInitializer>
</React.StrictMode>,
);

Expand Down
4 changes: 3 additions & 1 deletion front/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ self.addEventListener('fetch', (event: FetchEvent) => {
async function storeMessage(request: Request) {
const db = await openDB('myDB', 1, {
upgrade(db) {
db.createObjectStore('outbox', { autoIncrement: true, keyPath: 'id' });
if (!db.objectStoreNames.contains('outbox')) {
db.createObjectStore('outbox', { autoIncrement: true, keyPath: 'id' });
}
},
});

Expand Down
33 changes: 33 additions & 0 deletions front/src/utils/AppInitializer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { openDB } from 'idb';
import React, { ReactNode, useEffect } from 'react';

interface AppInitializerProps {
children: ReactNode;
}

const AppInitializer = ({ children }: AppInitializerProps) => {
useEffect(() => {
async function setupDatabase() {
try {
await openDB('myDB', 1, {
upgrade(db) {
if (!db.objectStoreNames.contains('outbox')) {
db.createObjectStore('outbox', {
autoIncrement: true,
keyPath: 'id',
});
}
},
});
} catch (error) {
console.error('Error setting up the database:', error);
}
}

setupDatabase();
}, []);

return <>{children}</>;
};

export default AppInitializer;

0 comments on commit 105749e

Please sign in to comment.