Skip to content

Commit 335c418

Browse files
authored
Add option for query id routing (#31)
1 parent 7dc3edb commit 335c418

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

.github/workflows/pages.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Deploy to GitHub Pages
2+
3+
on:
4+
workflow_dispatch:
5+
6+
permissions:
7+
contents: read
8+
pages: write
9+
id-token: write
10+
11+
concurrency:
12+
group: 'pages'
13+
cancel-in-progress: true
14+
15+
jobs:
16+
deploy:
17+
environment:
18+
name: github-pages
19+
url: ${{ steps.deployment.outputs.page_url }}
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
- name: Set up Node
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: lts/*
28+
cache: 'npm'
29+
- name: Install dependencies
30+
run: yarn install --frozen-lockfile
31+
- name: Build
32+
run: yarn build
33+
env:
34+
#VITE_BYTEBIN_URL: "https://your-bytebin.example.com/"
35+
VITE_USE_QUERY_ROUTING: "true" # required for github pages
36+
- name: Setup Pages
37+
uses: actions/configure-pages@v5
38+
- name: Upload artifact
39+
uses: actions/upload-pages-artifact@v3
40+
with:
41+
# Upload dist folder
42+
path: './dist'
43+
- name: Deploy to GitHub Pages
44+
id: deployment
45+
uses: actions/deploy-pages@v4

src/App.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Editor from './components/Editor';
55
import usePreference from './hooks/usePreference.ts';
66
import themes, { Themes } from './style/themes.ts';
77
import { loadFromBytebin } from './util/storage';
8+
import { useQueryRouting } from './util/constants';
89

910
const INITIAL = Symbol();
1011
const LOADING = Symbol();
@@ -81,10 +82,10 @@ function get404Message(pasteId: string) {
8182
}
8283

8384
function getPasteIdFromUrl() {
84-
const path = window.location.pathname;
85-
if (path && /^\/[a-zA-Z0-9]+$/.test(path)) {
86-
return path.substring(1);
87-
} else {
88-
return undefined;
85+
if (useQueryRouting) {
86+
return new URLSearchParams(window.location.search).get('id') ?? undefined;
8987
}
88+
89+
const path = window.location.pathname;
90+
return /^\/[a-zA-Z0-9]+$/.test(path) ? path.substring(1) : undefined;
9091
}

src/components/EditorControls.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import styled from 'styled-components';
66
import themes, { Themes } from '../style/themes';
77
import { languages } from '../util/highlighting';
88
import { saveToBytebin } from '../util/storage';
9+
import { useQueryRouting } from '../util/constants';
910
import Button from './Button';
1011
import { ResetFunction } from './Editor';
1112
import MenuButton from './MenuButton';
@@ -59,9 +60,15 @@ export default function EditorControls({
5960
setSaving(false);
6061
setRecentlySaved(true);
6162
if (pasteId) {
62-
history.replace({
63-
pathname: pasteId,
64-
});
63+
if (useQueryRouting) {
64+
history.replace({
65+
search: `?id=${pasteId}`,
66+
});
67+
} else {
68+
history.replace({
69+
pathname: pasteId,
70+
});
71+
}
6572
copy(window.location.href);
6673
document.title = 'paste | ' + pasteId;
6774
}

src/util/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const bytebinUrl =
22
import.meta.env.VITE_BYTEBIN_URL || 'https://bytebin.lucko.me/';
33
export const postUrl = bytebinUrl + 'post';
4+
export const useQueryRouting =
5+
import.meta.env.VITE_USE_QUERY_ROUTING === 'true';

src/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
interface ImportMetaEnv {
44
readonly VITE_BYTEBIN_URL?: string;
5+
readonly VITE_USE_QUERY_ROUTING?: string;
56
}
67

78
interface ImportMeta {

0 commit comments

Comments
 (0)