stacker.news/pages/_app.js

102 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-04-14 00:57:32 +00:00
import '../styles/globals.scss'
import { ApolloProvider, gql } from '@apollo/client'
2021-11-09 17:38:58 +00:00
import { MeProvider } from '../components/me'
2021-07-15 20:49:13 +00:00
import PlausibleProvider from 'next-plausible'
2021-09-30 15:46:58 +00:00
import getApolloClient from '../lib/apollo'
2021-11-13 13:28:08 +00:00
import NextNProgress from 'nextjs-progressbar'
2021-11-28 17:29:17 +00:00
import { PriceProvider } from '../components/price'
2022-05-19 16:50:38 +00:00
import Head from 'next/head'
import { useRouter } from 'next/dist/client/router'
import { useEffect } from 'react'
import { ShowModalProvider } from '../components/modal'
2023-02-08 21:56:43 +00:00
import ErrorBoundary from '../components/error-boundary'
2023-07-05 14:47:44 +00:00
import { LightningProvider } from '../components/lightning'
Service worker rework, Web Target Share API & Web Push API (#324) * npm uninstall next-pwa next-pwa was last updated in August 2022. There is also an issue which mentions that next-pwa is abandoned (?): https://github.com/shadowwalker/next-pwa/issues/482 But the main reason for me uninstalling it is that it adds a lot of preconfigured stuff which is not necessary for us. It even lead to a bug since pages were cached without our knowledge. So I will go with a different PWA approach. This different approach should do the following: - make it more transparent what the service worker is doing - gives us more control to configure the service worker and thus making it easier * Use workbox-webpack-plugin Every other plugin (`next-offline`, `next-workbox-webpack-plugin`, `next-with-workbox`, ...) added unnecessary configuration which felt contrary to how PWAs should be built. (PWAs should progressivly enhance the website in small steps, see https://web.dev/learn/pwa/getting-started/#focus-on-a-feature) These default configurations even lead to worse UX since they made invalid assumptions about stacker.news: We _do not_ want to cache our start url and we _do not_ want to cache anything unless explicitly told to. Almost every page on SN should be fresh for the best UX. To achieve this, by default, the service worker falls back to the network (as if the service worker wasn't there). Therefore, this should be the simplest configuration with a valid precache and cache busting support. In the future, we can try to use prefetching to improve performance of navigation requests. * Add support for Web Share Target API See https://developer.chrome.com/articles/web-share-target/ * Use Web Push API for push notifications I followed this (very good!) guide: https://web.dev/notifications/ * Refactor code related to Web Push * Send push notification to users on events * Merge notifications * Send notification to author of every parent recursively * Remove unused userId param in savePushSubscription As it should be, the user id is retrieved from the authenticated user in the backend. * Resubscribe user if push subscription changed * Update old subscription if oldEndpoint was given * Allow users to unsubscribe * Use LTREE operator instead of recursive query * Always show checkbox for push notifications * Justify checkbox to end * Update title of first push notification * Fix warning from uncontrolled to controlled * Add comment about Notification.requestPermission * Fix timestamp * Catch error on push subscription toggle * Wrap function bodies in try/catch * Use Promise.allSettled * Filter subscriptions by user notification settings * Fix user notification filter * Use skipWaiting --------- Co-authored-by: ekzyis <ek@stacker.news>
2023-07-04 19:36:07 +00:00
import { ServiceWorkerProvider } from '../components/serviceworker'
2023-07-24 18:35:05 +00:00
const SSR = typeof window === 'undefined'
function writeQuery (client, apollo, data) {
if (apollo && data) {
client.writeQuery({
query: gql`${apollo.query}`,
2023-07-25 14:14:45 +00:00
data,
variables: apollo.variables,
2023-07-24 18:35:05 +00:00
broadcast: !SSR,
overwrite: SSR
})
}
}
2021-03-22 20:36:10 +00:00
function MyApp ({ Component, pageProps: { ...props } }) {
2021-10-26 20:49:37 +00:00
const client = getApolloClient()
const router = useRouter()
2022-11-06 17:28:58 +00:00
useEffect(() => {
// HACK: 'cause there's no way to tell Next to skip SSR
// So every page load, we modify the route in browser history
// to point to the same page but without SSR, ie ?nodata=true
// this nodata var will get passed to the server on back/foward and
// 1. prevent data from reloading and 2. perserve scroll
// (2) is not possible while intercepting nav with beforePopState
2023-07-31 14:10:45 +00:00
if (router.query.nodata || !router.isReady) return
2023-07-26 00:45:35 +00:00
router.replace({
pathname: router.pathname,
query: { ...router.query, nodata: true }
}, router.asPath, { ...router.options, shallow: true }).catch((e) => {
// workaround for https://github.com/vercel/next.js/issues/37362
if (!e.cancelled) {
throw e
}
})
2023-07-26 00:45:35 +00:00
}, [router.pathname, router.query])
2021-11-09 22:43:56 +00:00
2021-10-26 20:49:37 +00:00
/*
If we are on the client, we populate the apollo cache with the
ssr data
*/
const { apollo, ssrData, me, price, ...otherProps } = props
// if we are on the server, useEffect won't run
2023-07-24 18:35:05 +00:00
if (SSR && client) {
writeQuery(client, apollo, ssrData)
2021-10-26 20:49:37 +00:00
}
useEffect(() => {
writeQuery(client, apollo, ssrData)
}, [client, apollo, ssrData])
2021-04-12 18:05:09 +00:00
return (
2021-11-13 13:28:08 +00:00
<>
<NextNProgress
2023-07-24 18:35:05 +00:00
color='var(--bs-primary)'
2021-11-13 13:28:08 +00:00
startPosition={0.3}
stopDelayMs={200}
height={2}
showOnShallow={false}
2021-11-13 13:28:08 +00:00
options={{ showSpinner: false }}
/>
2022-05-19 16:50:38 +00:00
<Head>
<meta name='viewport' content='initial-scale=1.0, width=device-width' />
</Head>
2023-02-08 21:56:43 +00:00
<ErrorBoundary>
<PlausibleProvider domain='stacker.news' trackOutboundLinks>
<ApolloProvider client={client}>
<MeProvider me={me}>
<ServiceWorkerProvider>
<PriceProvider price={price}>
<LightningProvider>
<ShowModalProvider>
<Component ssrData={ssrData} {...otherProps} />
</ShowModalProvider>
</LightningProvider>
</PriceProvider>
</ServiceWorkerProvider>
</MeProvider>
</ApolloProvider>
2023-02-08 21:56:43 +00:00
</PlausibleProvider>
</ErrorBoundary>
2021-11-13 13:28:08 +00:00
</>
2021-04-12 18:05:09 +00:00
)
2021-03-22 20:36:10 +00:00
}
export default MyApp