Skip to content

Commit

Permalink
feat: migrate project to Remix's Vite plugin (#81)
Browse files Browse the repository at this point in the history
* Migrate from Remix compiler to Vite

* Update remix-pwa stuff

* Fix CSS URL imports

* Fix warning about importing from /public/favicon.ico

* Reorder imports

* Remove @epic-web/remember

* Make favicon destination directory path more clear

* Write sw.js directly to build/client

* Update clean script
  • Loading branch information
wKovacs64 authored Feb 27, 2024
1 parent 8bc4138 commit 85d9db8
Show file tree
Hide file tree
Showing 15 changed files with 522 additions and 318 deletions.
8 changes: 6 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ module.exports = {
'import/no-unresolved': [
'error',
{
ignore: ['^~/icons/icon', './icons-sprite.svg'],
ignore: [
'build/server/index.js',
'^~/icons/icon',
'./icons-sprite.svg',
],
},
],
'import/order': [
Expand All @@ -100,7 +104,7 @@ module.exports = {

// Node
{
files: ['.eslintrc.cjs', 'server/**/*.ts', '**/*.server.ts'],
files: ['.eslintrc.cjs', 'server.ts', '**/*.server.ts'],
env: {
node: true,
},
Expand Down
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,14 @@ $RECYCLE.BIN/
# Manual exclusions
!**/.gitkeep

# Remix on fly.io
# Remix
/.cache
/build
/public/build
.env

# Prisma
/prisma/*.db
/prisma/*.db-journal

# ShafSpecs/remix-pwa
/public/sw.js

# Generated SVG icons sprite and component
/app/icons
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ WORKDIR /app

COPY --from=production-deps /app/node_modules /app/node_modules
COPY --from=build /app/node_modules/.prisma /app/node_modules/.prisma
COPY --from=build /app/build /app/build
COPY --from=build /app/public /app/public
COPY --from=build /app/build/server /app/build/server
COPY --from=build /app/build/client /app/build/client

# prepare litefs
ENV LITEFS_DIR="/litefs"
Expand Down
3 changes: 0 additions & 3 deletions app/entry.client.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { RemixBrowser } from '@remix-run/react';
import { hydrateRoot } from 'react-dom/client';
import { loadServiceWorker } from '@remix-pwa/sw';
import { requestIdleCallbackShim } from '~/utils/request-idle-callback-shim';

requestIdleCallbackShim(() => {
Expand All @@ -14,5 +13,3 @@ requestIdleCallbackShim(() => {
);
});
});

loadServiceWorker({ serviceWorkerUrl: '/sw.js' });
100 changes: 44 additions & 56 deletions app/entry.worker.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,57 @@
/// <reference lib="WebWorker" />

import { Storage } from '@remix-pwa/cache';
import { cacheFirst, networkFirst } from '@remix-pwa/strategy';
import type { DefaultFetchHandler } from '@remix-pwa/sw';
import { RemixNavigationHandler, logger, matchRequest } from '@remix-pwa/sw';
import {
EnhancedCache,
logger,
clearUpOldCaches,
NavigationHandler,
} from '@remix-pwa/sw';

declare let self: ServiceWorkerGlobalScope;

const PAGES = 'page-cache';
const DATA = 'data-cache';
const ASSETS = 'assets-cache';
const CURRENT_CACHE_VERSION = 'v2';

// Open the caches and wrap them in `RemixCache` instances.
const dataCache = Storage.open(DATA, {
ttl: 60 * 60 * 24 * 7 * 1_000, // 7 days
});
const documentCache = Storage.open(PAGES);
const assetCache = Storage.open(ASSETS);
// const assetCache = new EnhancedCache('remix-assets', {
// version: CURRENT_CACHE_VERSION,
// strategy: 'CacheFirst',
// strategyOptions: {
// maxEntries: 2,
// maxAgeSeconds: 60,
// cacheableResponse: false,
// },
// });

self.addEventListener('install', (event: ExtendableEvent) => {
logger.log('Service worker installed');
event.waitUntil(self.skipWaiting());
});

self.addEventListener('activate', (event: ExtendableEvent) => {
logger.log('Service worker activated');
event.waitUntil(self.clients.claim());
logger.log('installing service worker');
event.waitUntil(
Promise.all([
self.skipWaiting(),
// assetCache.preCacheUrls(['/entry.worker.css']),
]),
);
});

const dataHandler = networkFirst({
cache: dataCache,
});

const assetsHandler = cacheFirst({
cache: assetCache,
cacheQueryOptions: {
ignoreSearch: true,
ignoreVary: true,
},
});

// The default fetch event handler will be invoke if the
// route is not matched by any of the worker action/loader.
export const defaultFetchHandler: DefaultFetchHandler = ({
context,
request,
}) => {
const type = matchRequest(request);

if (type === 'asset') {
return assetsHandler(context.event.request);
}

if (type === 'loader') {
return dataHandler(context.event.request);
}

return context.fetchFromServer();
};

const handler = new RemixNavigationHandler({
dataCache,
documentCache,
self.addEventListener('activate', (event) => {
logger.log(self.clients, 'manifest:\n', self.__workerManifest);
event.waitUntil(
Promise.all([
clearUpOldCaches(['remix-assets'], CURRENT_CACHE_VERSION),
]).then(() => {
self.clients.claim();
}),
);
});

self.addEventListener('message', (event) => {
event.waitUntil(handler.handle(event));
new NavigationHandler({
documentCache: new EnhancedCache('remix-document', {
version: CURRENT_CACHE_VERSION,
strategy: 'CacheFirst',
strategyOptions: {
maxEntries: 10,
maxAgeSeconds: 60,
cacheableResponse: {
statuses: [200],
},
},
}),
});
File renamed without changes.
13 changes: 5 additions & 8 deletions app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { cssBundleHref } from '@remix-run/css-bundle';
import { json, type LinksFunction, type MetaFunction } from '@remix-run/node';
import {
Links,
Expand All @@ -7,20 +6,20 @@ import {
Scripts,
ScrollRestoration,
} from '@remix-run/react';
import sourceSans3Latin300 from '@fontsource/source-sans-3/latin-300.css';
import sourceSans3Latin400 from '@fontsource/source-sans-3/latin-400.css';
import { useSWEffect, LiveReload } from '@remix-pwa/sw';
import { useSWEffect } from '@remix-pwa/sw';
import sourceSans3Latin300 from '@fontsource/source-sans-3/latin-300.css?url';
import sourceSans3Latin400 from '@fontsource/source-sans-3/latin-400.css?url';
import appStylesUrl from '~/styles/app.css?url';
import { iconsSpriteHref } from '~/icons/icon';
import faviconIcoUrl from '~/images/favicon.ico';
import icon32Url from '~/images/icon-32x32.png';
import appleTouchIconUrl from '~/images/apple-touch-icon.png';
import appStylesUrl from '~/styles/app.css';
import { backgroundImageStyles } from '~/styles/background-image';
import { getEnvVars } from '~/utils/env.server';
import SkipNavLink from '~/core/skip-nav-link';
import Header from '~/core/header';
import Footer from '~/core/footer';
import Breadcrumbs from '~/navigation/breadcrumbs';
import faviconIcoUrl from '../public/favicon.ico';
import type { AppRouteHandle } from './types';

export async function loader() {
Expand Down Expand Up @@ -68,7 +67,6 @@ export const handle: AppRouteHandle = {
};

export const links: LinksFunction = () => [
...(cssBundleHref ? [{ rel: 'stylesheet', href: cssBundleHref }] : []),
{ rel: 'preload', href: iconsSpriteHref, as: 'image' },
{
rel: 'preconnect',
Expand Down Expand Up @@ -114,7 +112,6 @@ export default function App() {
<Footer />
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
Expand Down
9 changes: 3 additions & 6 deletions app/utils/db.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { remember } from '@epic-web/remember';
import { PrismaClient } from '@prisma/client';

export const prisma = remember('prisma', () => {
const client = new PrismaClient();
client.$connect();
return client;
});
export const prisma = new PrismaClient();

prisma.$connect();
2 changes: 1 addition & 1 deletion remix.env.d.ts → env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/// <reference types="@remix-run/dev" />
/// <reference types="vite/client" />
/// <reference types="@remix-run/node" />
Loading

0 comments on commit 85d9db8

Please sign in to comment.