Skip to content

Commit

Permalink
Add support for PWA (LemmyNet#1005)
Browse files Browse the repository at this point in the history
* Add logic for dynamically generating web manifest

* Make PWA icon get autogenerated

* Make service worker work

* Tweak things for PWA

* Handle apple icons and refactor

* Update prod dockerfile

* Remove jimp

* Remove unnecessary option

* Use different function syntax
  • Loading branch information
SleeplessOne1917 authored May 12, 2023
1 parent c5fd084 commit b19b51c
Show file tree
Hide file tree
Showing 15 changed files with 1,076 additions and 228 deletions.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ RUN curl -sf https://gobinaries.com/tj/node-prune | sh

WORKDIR /usr/src/app

ENV npm_config_target_arch=x64
ENV npm_config_target_platform=linux
ENV npm_config_target_libc=musl

# Cache deps
COPY package.json yarn.lock ./
RUN yarn install --production --ignore-scripts --prefer-offline --pure-lockfile
RUN yarn --production --prefer-offline --pure-lockfile

# Build
COPY generate_translations.js \
Expand All @@ -22,7 +26,7 @@ COPY .git .git
# Set UI version
RUN echo "export const VERSION = '$(git describe --tag)';" > "src/shared/version.ts"

RUN yarn install --production --ignore-scripts --prefer-offline
RUN yarn --production --prefer-offline
RUN yarn build:prod

# Prune the image
Expand Down
10 changes: 7 additions & 3 deletions dev.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ RUN apk update && apk add curl yarn python3 build-base gcc wget git --no-cache

WORKDIR /usr/src/app

ENV npm_config_target_arch=x64
ENV npm_config_target_platform=linux
ENV npm_config_target_libc=musl

# Cache deps
COPY package.json yarn.lock ./
RUN yarn install --ignore-scripts --prefer-offline --pure-lockfile
RUN yarn --prefer-offline --pure-lockfile

# Build
COPY generate_translations.js \
Expand All @@ -20,7 +24,7 @@ COPY src src
# Set UI version
RUN echo "export const VERSION = 'dev';" > "src/shared/version.ts"

RUN yarn install --ignore-scripts --prefer-offline
RUN yarn --prefer-offline
RUN yarn build:dev

FROM node:alpine as runner
Expand All @@ -29,4 +33,4 @@ COPY --from=builder /usr/src/app/node_modules /app/node_modules

EXPOSE 1234
WORKDIR /app
CMD node dist/js/server.js
CMD node dist/js/server.js
2 changes: 1 addition & 1 deletion lemmy-translations
Submodule lemmy-translations updated 54 files
+0 −13 .woodpecker.yml
+1 −3 README.md
+16 −16 email/fi.json
+11 −11 email/it.json
+16 −16 email/ko.json
+16 −16 email/pt.json
+486 −486 translations/ar.json
+78 −78 translations/as.json
+393 −393 translations/bg.json
+106 −106 translations/bn.json
+309 −309 translations/ca.json
+373 −373 translations/cs.json
+28 −28 translations/cy.json
+281 −478 translations/da.json
+357 −357 translations/de.json
+463 −463 translations/el.json
+481 −480 translations/en.json
+475 −478 translations/eo.json
+475 −478 translations/es.json
+439 −439 translations/eu.json
+297 −297 translations/fa.json
+467 −467 translations/fi.json
+376 −376 translations/fr.json
+330 −330 translations/ga.json
+455 −455 translations/gl.json
+35 −35 translations/got.json
+64 −64 translations/hi.json
+414 −414 translations/hu.json
+398 −398 translations/id.json
+419 −419 translations/it.json
+415 −458 translations/ja.json
+227 −227 translations/ka.json
+440 −440 translations/ko.json
+278 −278 translations/lt.json
+182 −182 translations/ml.json
+15 −15 translations/mnc.json
+43 −43 translations/nb_NO.json
+393 −393 translations/nl.json
+412 −412 translations/oc.json
+478 −478 translations/pl.json
+414 −414 translations/pt.json
+460 −460 translations/pt_BR.json
+410 −410 translations/ru.json
+28 −28 translations/sk.json
+239 −239 translations/sq.json
+56 −56 translations/sr_Latn.json
+360 −360 translations/sv.json
+139 −139 translations/th.json
+250 −250 translations/tr.json
+323 −323 translations/uk.json
+376 −376 translations/vi.json
+379 −379 translations/zh.json
+396 −396 translations/zh_Hant.json
+140 −140 translators.json
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"classnames": "^2.3.1",
"clean-webpack-plugin": "^4.0.0",
"copy-webpack-plugin": "^11.0.0",
"cross-fetch": "^3.1.5",
"css-loader": "^6.7.3",
"emoji-mart": "^5.4.0",
"emoji-short-name": "^2.0.0",
Expand Down Expand Up @@ -76,6 +77,7 @@
"sass": "^1.62.1",
"sass-loader": "^13.2.2",
"serialize-javascript": "^6.0.1",
"sharp": "^0.32.1",
"tippy.js": "^6.3.7",
"toastify-js": "^1.12.0",
"tributejs": "^5.1.3",
Expand Down Expand Up @@ -113,7 +115,8 @@
"style-loader": "^3.3.2",
"terser": "^5.17.3",
"typescript": "^5.0.4",
"webpack-dev-server": "4.15.0"
"webpack-dev-server": "4.15.0",
"service-worker-webpack": "^1.0.0"
},
"engines": {
"node": ">=8.9.0"
Expand Down
49 changes: 0 additions & 49 deletions src/assets/manifest.webmanifest

This file was deleted.

122 changes: 108 additions & 14 deletions src/server/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import express from "express";
import fs from "fs";
import { existsSync } from "fs";
import { readdir, readFile } from "fs/promises";
import { IncomingHttpHeaders } from "http";
import { Helmet } from "inferno-helmet";
import { matchPath, StaticRouter } from "inferno-router";
import { renderToString } from "inferno-server";
import IsomorphicCookie from "isomorphic-cookie";
import { GetSite, GetSiteResponse, LemmyHttp } from "lemmy-js-client";
import { GetSite, GetSiteResponse, LemmyHttp, Site } from "lemmy-js-client";
import path from "path";
import process from "process";
import serialize from "serialize-javascript";
import sharp from "sharp";
import { App } from "../shared/components/app/app";
import { httpBaseInternal } from "../shared/env";
import { getHttpBase, getHttpBaseInternal } from "../shared/env";
import {
ILemmyConfig,
InitialFetchRequest,
IsoData,
} from "../shared/interfaces";
import { routes } from "../shared/routes";
import { initializeSite } from "../shared/utils";
import { favIconPngUrl, favIconUrl, initializeSite } from "../shared/utils";

const server = express();
const [hostname, port] = process.env["LEMMY_UI_HOST"]
Expand Down Expand Up @@ -54,6 +56,11 @@ Disallow: /password_change
Disallow: /search/
`;

server.get("/service-worker.js", async (_req, res) => {
res.setHeader("Content-Type", "application/javascript");
res.sendFile(path.resolve("./dist/service-worker.js"));
});

server.get("/robots.txt", async (_req, res) => {
res.setHeader("content-type", "text/plain; charset=utf-8");
res.send(robotstxt);
Expand All @@ -67,25 +74,25 @@ server.get("/css/themes/:name", async (req, res) => {
}

const customTheme = path.resolve(`./${extraThemesFolder}/${theme}`);
if (fs.existsSync(customTheme)) {
if (existsSync(customTheme)) {
res.sendFile(customTheme);
} else {
const internalTheme = path.resolve(`./dist/assets/css/themes/${theme}`);

// If the theme doesn't exist, just send litely
if (fs.existsSync(internalTheme)) {
if (existsSync(internalTheme)) {
res.sendFile(internalTheme);
} else {
res.sendFile(path.resolve("./dist/assets/css/themes/litely.css"));
}
}
});

function buildThemeList(): string[] {
let themes = ["darkly", "darkly-red", "litely", "litely-red"];
if (fs.existsSync(extraThemesFolder)) {
let dirThemes = fs.readdirSync(extraThemesFolder);
let cssThemes = dirThemes
async function buildThemeList(): Promise<string[]> {
const themes = ["darkly", "darkly-red", "litely", "litely-red"];
if (existsSync(extraThemesFolder)) {
const dirThemes = await readdir(extraThemesFolder);
const cssThemes = dirThemes
.filter(d => d.endsWith(".css"))
.map(d => d.replace(".css", ""));
themes.push(...cssThemes);
Expand All @@ -95,7 +102,7 @@ function buildThemeList(): string[] {

server.get("/css/themelist", async (_req, res) => {
res.type("json");
res.send(JSON.stringify(buildThemeList()));
res.send(JSON.stringify(await buildThemeList()));
});

// server.use(cookieParser());
Expand All @@ -110,7 +117,7 @@ server.get("/*", async (req, res) => {
const promises: Promise<any>[] = [];

const headers = setForwardedHeaders(req.headers);
const client = new LemmyHttp(httpBaseInternal, headers);
const client = new LemmyHttp(getHttpBaseInternal(), headers);

// Get site data first
// This bypasses errors, so that the client can hit the error on its own,
Expand Down Expand Up @@ -180,6 +187,23 @@ server.get("/*", async (req, res) => {

const config: ILemmyConfig = { wsHost: process.env.LEMMY_UI_LEMMY_WS_HOST };

const appleTouchIcon = site.site_view.site.icon
? `data:image/png;base64,${sharp(
await fetchIconPng(site.site_view.site.icon)
)
.resize(180, 180)
.extend({
bottom: 20,
top: 20,
left: 20,
right: 20,
background: "#222222",
})
.png()
.toBuffer()
.then(buf => buf.toString("base64"))}`
: favIconPngUrl;

res.send(`
<!DOCTYPE html>
<html ${helmet.htmlAttributes.toString()} lang="en">
Expand All @@ -200,9 +224,19 @@ server.get("/*", async (req, res) => {
<meta name="Description" content="Lemmy">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link
id="favicon"
rel="shortcut icon"
type="image/x-icon"
href=${site.site_view.site.icon ?? favIconUrl}
/>
<!-- Web app manifest -->
<link rel="manifest" href="/static/assets/manifest.webmanifest">
<link rel="manifest" href="data:application/manifest+json;base64,${await generateManifestBase64(
site.site_view.site
)}">
<link rel="apple-touch-icon" href=${appleTouchIcon} />
<link rel="apple-touch-startup-image" href=${appleTouchIcon} />
<!-- Styles -->
<link rel="stylesheet" type="text/css" href="/static/styles/styles.css" />
Expand Down Expand Up @@ -267,3 +301,63 @@ function removeParam(url: string, parameter: string): string {
.replace(new RegExp("[?&]" + parameter + "=[^&#]*(#.*)?$"), "$1")
.replace(new RegExp("([?&])" + parameter + "=[^&]*&"), "$1");
}

const iconSizes = [72, 96, 128, 144, 152, 192, 384, 512];
const defaultLogoPathDirectory = path.join(
process.cwd(),
"dist",
"assets",
"icons"
);

export async function generateManifestBase64(site: Site) {
const url = (
process.env.NODE_ENV === "development"
? "http://localhost:1236/"
: getHttpBase()
).replace(/\/$/g, "");
const icon = site.icon ? await fetchIconPng(site.icon) : null;

const manifest = {
name: site.name,
description: site.description ?? "A link aggregator for the fediverse",
start_url: url,
scope: url,
display: "standalone",
id: "/",
background_color: "#222222",
theme_color: "#222222",
icons: await Promise.all(
iconSizes.map(async size => {
let src = await readFile(
path.join(defaultLogoPathDirectory, `icon-${size}x${size}.png`)
).then(buf => buf.toString("base64"));

if (icon) {
src = await sharp(icon)
.resize(size, size)
.png()
.toBuffer()
.then(buf => buf.toString("base64"));
}

return {
sizes: `${size}x${size}`,
type: "image/png",
src: `data:image/png;base64,${src}`,
purpose: "any maskable",
};
})
),
};

return Buffer.from(JSON.stringify(manifest)).toString("base64");
}

async function fetchIconPng(iconUrl: string) {
return await fetch(
iconUrl.replace(/https?:\/\/localhost:\d+/g, getHttpBaseInternal())
)
.then(res => res.blob())
.then(blob => blob.arrayBuffer());
}
28 changes: 0 additions & 28 deletions src/service-worker.ts

This file was deleted.

Loading

0 comments on commit b19b51c

Please sign in to comment.