-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
141 lines (128 loc) · 4.17 KB
/
Copy pathproxy.ts
File metadata and controls
141 lines (128 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { createServerClient } from "@supabase/ssr";
import { NextRequest, NextResponse } from "next/server";
const SW_CLEANUP_QUERY_PARAM = "devbcn-sw-cleanup";
function buildLegacyRegisterSwScript(): string {
return [
"(function () {",
" async function cleanup() {",
" try {",
" var hadRegistrations = false;",
" var hadCaches = false;",
"",
' if ("serviceWorker" in navigator) {',
" var registrations = await navigator.serviceWorker.getRegistrations();",
" hadRegistrations = registrations.length > 0;",
" await Promise.all(registrations.map(function (registration) {",
" return registration.unregister();",
" }));",
" }",
"",
' if ("caches" in window) {',
" var cacheNames = await caches.keys();",
" hadCaches = cacheNames.length > 0;",
" await Promise.all(cacheNames.map(function (cacheName) {",
" return caches.delete(cacheName);",
" }));",
" }",
"",
" if (hadRegistrations || hadCaches) {",
" var url = new URL(window.location.href);",
' if (url.searchParams.get("' + SW_CLEANUP_QUERY_PARAM + '") !== "1") {',
' url.searchParams.set("' + SW_CLEANUP_QUERY_PARAM + '", "1");',
" window.location.replace(url.toString());",
" }",
" }",
" } catch (error) {",
' console.error("[DevBcn] Legacy service worker cleanup failed:", error);',
" }",
" }",
"",
" void cleanup();",
"})();",
].join("\n");
}
function buildLegacyManifest(): string {
return JSON.stringify(
{
name: "DevBcn Conference",
short_name: "DevBcn",
description: "The biggest developer conference in Barcelona. Join hundreds of developers for cutting-edge talks, workshops, and networking.",
start_url: ".",
display: "standalone",
background_color: "#ffffff",
theme_color: "#000000",
icons: [
{
src: "/favicon.ico",
sizes: "64x64 32x32 24x24 16x16",
type: "image/x-icon",
},
{
src: "/assets/img/icons/logo192.png",
sizes: "192x192",
type: "image/png",
},
{
src: "/assets/img/icons/logo512.png",
sizes: "512x512",
type: "image/png",
},
{
src: "/assets/img/icons/maskable_icon_x192.png",
sizes: "192x192",
type: "image/png",
purpose: "maskable",
},
],
},
null,
2
);
}
async function updateSupabaseSession(request: NextRequest): Promise<NextResponse> {
const response = NextResponse.next({
request,
});
const supabase = createServerClient(process.env.NEXT_PUBLIC_STORAGE_SUPABASE_URL!, process.env.NEXT_PUBLIC_STORAGE_SUPABASE_ANON_KEY!, {
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => {
request.cookies.set(name, value);
response.cookies.set(name, value, options);
});
},
},
});
await supabase.auth.getUser();
return response;
}
export async function proxy(request: NextRequest): Promise<NextResponse> {
const { pathname } = request.nextUrl;
if (pathname.endsWith("/registerSW.js")) {
return new NextResponse(buildLegacyRegisterSwScript(), {
headers: {
"Cache-Control": "no-cache, no-store, must-revalidate",
"Clear-Site-Data": '"cache"',
"Content-Type": "application/javascript; charset=utf-8",
},
});
}
if (pathname.endsWith("/service-worker.js")) {
return NextResponse.rewrite(new URL("/sw.js", request.url));
}
if (pathname.endsWith("/manifest.json")) {
return new NextResponse(buildLegacyManifest(), {
headers: {
"Cache-Control": "no-cache, no-store, must-revalidate",
"Content-Type": "application/manifest+json; charset=utf-8",
},
});
}
return updateSupabaseSession(request);
}
export const config = {
matcher: ["/admin/:path*", "/sponsor/:path*", "/auth/:path*", "/registerSW.js", "/service-worker.js", "/manifest.json"],
};