-
Notifications
You must be signed in to change notification settings - Fork 39
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
Switch from NextJS -> Vite / plain React for demos #83
Merged
Merged
Changes from 12 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
aab8ce4
Switch from NextJS -> Vite for supabase-todolist demo.
rkistner 45ba14a
Some demo cleanup.
rkistner 5582445
Merge remote-tracking branch 'origin/main' into todolist-demo-vite
rkistner 58ac056
Fix null-check issues.
rkistner 1fe6521
Rename nextjs-supabase-todolist -> react-supabase-todolist.
rkistner 493979f
Add a basic Next.JS example again.
rkistner 6fb26d9
Migrate Yjs demo to vite.
rkistner 1c1de76
Rename yjs-nextjs-supabase-text-collab ->
rkistner f99714a
Add missing files.
rkistner 57aeb14
Merge remote-tracking branch 'origin/main' into todolist-demo-vite
rkistner 261b2a2
Fixes.
rkistner ddf48a0
Minor cleanup.
rkistner f82d6ad
let -> const
rkistner d14f9d9
Add back PWA support; fix readme.
rkistner 6353003
PWA support for Yjs demo.
rkistner 790206d
Tweaks.
rkistner e685599
Merge remote-tracking branch 'origin/todolist-demo-vite' into todolis…
rkistner 880ef16
Fix some null-check issues.
rkistner 19729bb
Use development tokens for example-nextjs.
rkistner d73eb06
Merge remote-tracking branch 'origin/main' into todolist-demo-vite
rkistner 33cdcc8
Bump some demo dependencies to avoid duplicates
rkistner 612b133
Fix layouts to not have navbar on login/register.
rkistner 2021010
Cleanup.
rkistner de63073
Update demos/example-nextjs/README.md
rkistner f8c211d
Merge remote-tracking branch 'origin/main' into todolist-demo-vite
rkistner 6cb64af
Fix null-check issue.
rkistner 1aed928
Add changeset.
rkistner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NEXT_PUBLIC_POWERSYNC_URL= | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# PowerSync Next.js example | ||
|
||
This example is built using [Next.js](https://nextjs.org/) and the [PowerSync JS web SDK](https://docs.powersync.com/client-sdk-references/js-web). | ||
|
||
To see it in action: | ||
|
||
1. Make sure to run `pnpm build:packages` in the root directory of this Git repo. | ||
2. `pnpm start` | ||
3. Open the localhost URL in the browser displayed in the terminal output. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use client'; | ||
|
||
import React, { useEffect } from 'react'; | ||
import { CircularProgress, Grid, ListItem, styled } from '@mui/material'; | ||
import { useRouter } from 'next/navigation'; | ||
import { usePowerSync, usePowerSyncWatchedQuery } from '@journeyapps/powersync-react'; | ||
|
||
export default function EntryPage() { | ||
const router = useRouter(); | ||
const db = usePowerSync(); | ||
const customers = usePowerSyncWatchedQuery('SELECT id, name FROM customers'); | ||
|
||
useEffect(() => { | ||
// Insert some test data | ||
const names = ['Fred', 'Willard', 'Tina', 'Jake', 'Corey']; | ||
const name = names[Math.floor(Math.random() * names.length)]; | ||
db.execute('INSERT INTO customers(id, name) VALUES(uuid(), ?)', [name]); | ||
return () => { }; | ||
}, []); | ||
|
||
return ( | ||
<S.MainGrid container> | ||
<S.CenteredGrid item xs={12} md={6} lg={5}> | ||
<div> | ||
<h1>Customers</h1> | ||
{ | ||
customers.map(c => | ||
<ListItem key={c.id}>{c.name}</ListItem> | ||
) | ||
} | ||
{customers.length == 0 ? <CircularProgress /> : []} | ||
|
||
</div> | ||
</S.CenteredGrid> | ||
</S.MainGrid> | ||
); | ||
} | ||
|
||
namespace S { | ||
export const CenteredGrid = styled(Grid)` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
`; | ||
|
||
export const MainGrid = styled(CenteredGrid)` | ||
min-height: 100vh; | ||
`; | ||
} |
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
demos/example-nextjs/src/components/providers/SystemProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use client'; | ||
|
||
import { AppSchema } from '@/library/powersync/AppSchema'; | ||
import { BackendConnector } from '@/library/powersync/BackendConnector'; | ||
import { PowerSyncContext } from '@journeyapps/powersync-react'; | ||
import { WASQLitePowerSyncDatabaseOpenFactory } from '@journeyapps/powersync-sdk-web'; | ||
import { CircularProgress } from '@mui/material'; | ||
import Logger from 'js-logger'; | ||
import React, { Suspense } from 'react'; | ||
|
||
Logger.useDefaults(); | ||
Logger.setLevel(Logger.DEBUG); | ||
|
||
const powerSync = new WASQLitePowerSyncDatabaseOpenFactory({ | ||
dbFilename: 'powersync2.db', | ||
schema: AppSchema, | ||
flags: { | ||
disableSSRWarning: true | ||
} | ||
}).getInstance() | ||
const connector = new BackendConnector(); | ||
|
||
powerSync.connect(connector); | ||
|
||
export const SystemProvider = ({ children }: { children: React.ReactNode }) => { | ||
return ( | ||
<Suspense fallback={<CircularProgress />}> | ||
<PowerSyncContext.Provider value={powerSync}> | ||
{children} | ||
</PowerSyncContext.Provider> | ||
</Suspense> | ||
); | ||
}; | ||
|
||
export default SystemProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { column, Schema, TableV2 } from '@journeyapps/powersync-sdk-web'; | ||
|
||
const customers = new TableV2({ | ||
name: column.text, | ||
created_at: column.text | ||
}); | ||
|
||
export const AppSchema = new Schema({ | ||
customers | ||
}); | ||
|
||
export type Database = (typeof AppSchema)['types']; | ||
export type Customer = Database['customers']; |
62 changes: 62 additions & 0 deletions
62
demos/example-nextjs/src/library/powersync/BackendConnector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { AbstractPowerSyncDatabase, BaseObserver, PowerSyncBackendConnector } from '@journeyapps/powersync-sdk-web'; | ||
|
||
export type BackendConfig = { | ||
powersyncUrl: string; | ||
}; | ||
|
||
export type BackendConnectorListener = { | ||
initialized: () => void; | ||
}; | ||
|
||
export class BackendConnector extends BaseObserver<BackendConnectorListener> implements PowerSyncBackendConnector { | ||
private _config: BackendConfig; | ||
|
||
constructor() { | ||
super(); | ||
this._config = { | ||
powersyncUrl: process.env.NEXT_PUBLIC_POWERSYNC_URL! | ||
}; | ||
} | ||
|
||
async fetchCredentials() { | ||
return { | ||
endpoint: this._config.powersyncUrl, | ||
token: '' // TODO: Implement | ||
}; | ||
} | ||
|
||
async uploadData(database: AbstractPowerSyncDatabase): Promise<void> { | ||
const transaction = await database.getNextCrudTransaction(); | ||
|
||
if (!transaction) { | ||
return; | ||
} | ||
|
||
try { | ||
// TODO: Upload here | ||
|
||
await transaction.complete(); | ||
} catch (error: any) { | ||
if (shouldDiscardDataOnError(error)) { | ||
/** | ||
* Instead of blocking the queue with these errors, discard the (rest of the) transaction. | ||
* | ||
* Note that these errors typically indicate a bug in the application. | ||
* If protecting against data loss is important, save the failing records | ||
* elsewhere instead of discarding, and/or notify the user. | ||
*/ | ||
console.error(`Data upload error - discarding`, error); | ||
await transaction.complete(); | ||
} else { | ||
// Error may be retryable - e.g. network error or temporary server error. | ||
// Throwing an error here causes this call to be retried after a delay. | ||
throw error; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function shouldDiscardDataOnError(error: any) { | ||
// TODO: Ignore non-retryable errors here | ||
return false; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to mention setting this field (and renaming this file) in the Readme?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually getting sync to work would also require setting up a connector. The other two examples (example-vite and example-webpack) also don't have anything for that.
Do you think it's worth expanding all those examples, perhaps with instructions using temporary development tokens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As newcomer, fake auth connection is good for learning and testing purposes.