Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update dependencies and posthog fixes #374

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/providers/posthog-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const PostHogCustomProvider = ({
if (typeof window !== "undefined" && posthogConfig) {
posthog.init(posthogConfig.key, {
api_host: posthogConfig.host,
ui_host: "https://eu.posthog.com",
disable_session_recording: true,
// Enable debug mode in development
loaded: (posthog) => {
Expand Down
26 changes: 26 additions & 0 deletions lib/middleware/posthog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextRequest, NextResponse } from "next/server";

export default async function PostHogMiddleware(req: NextRequest) {
let url = req.nextUrl.clone();
const hostname = url.pathname.startsWith("/ingest/static/")
? "eu-assets.i.posthog.com"
: "eu.i.posthog.com";
const requestHeaders = new Headers(req.headers);
requestHeaders.set("host", hostname);

// Handle OPTIONS method for CORS preflight
if (req.method === "OPTIONS") {
return new NextResponse("", {
status: 200,
});
}

url.protocol = "https";
url.hostname = hostname;
url.port = "443";
url.pathname = url.pathname.replace(/^\/ingest/, "");

return NextResponse.rewrite(url, {
headers: requestHeaders,
});
}
12 changes: 11 additions & 1 deletion lib/swr/use-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ export function useDocumentProcessingStatus(documentVersionId: string) {

export function useDocumentThumbnail(pageNumber: number, documentId: string) {
const { data, error } = useSWR<{ imageUrl: string }>(
`/api/jobs/get-thumbnail?documentId=${documentId}&pageNumber=${pageNumber}`,
pageNumber === 0
? null
: `/api/jobs/get-thumbnail?documentId=${documentId}&pageNumber=${pageNumber}`,
fetcher,
{
dedupingInterval: 1200000,
Expand All @@ -136,6 +138,14 @@ export function useDocumentThumbnail(pageNumber: number, documentId: string) {
},
);

if (pageNumber === 0) {
return {
data: null,
loading: false,
error: null,
};
}

return {
data,
loading: !error && !data,
Expand Down
18 changes: 16 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
import AppMiddleware from "@/lib/middleware/app";
import DomainMiddleware from "@/lib/middleware/domain";
import { BLOCKED_PATHNAMES } from "./lib/constants";
import PostHogMiddleware from "./lib/middleware/posthog";

function isAnalyticsPath(path: string) {
// Create a regular expression
// ^ - asserts position at start of the line
// /ingest/ - matches the literal string "/ingest/"
// .* - matches any character (except for line terminators) 0 or more times
const pattern = /^\/ingest\/.*/;

return pattern.test(path);
}

export const config = {
matcher: [
Expand All @@ -12,16 +23,19 @@ export const config = {
* 3. /_static (inside /public)
* 4. /_vercel (Vercel internals)
* 5. /favicon.ico, /sitemap.xml (static files)
* 6. ingest (analytics)
*/
"/((?!api/|_next/|_static|_vercel|ingest|favicon.ico|sitemap.xml).*)",
"/((?!api/|_next/|_static|_vercel|favicon.ico|sitemap.xml).*)",
],
};

export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
const path = req.nextUrl.pathname;
const host = req.headers.get("host");

if (isAnalyticsPath(path)) {
return PostHogMiddleware(req);
}

if (
(process.env.NODE_ENV === "development" && host?.includes(".local")) ||
(process.env.NODE_ENV !== "development" &&
Expand Down
9 changes: 1 addition & 8 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,7 @@ const nextConfig = {
],
},
transpilePackages: ["@trigger.dev/react"],
async rewrites() {
return [
{
source: "/ingest/:path*",
destination: "https://eu.posthog.com/:path*",
},
];
},
skipTrailingSlashRedirect: true,
experimental: {
outputFileTracingIncludes: {
"/api/mupdf/*": ["./node_modules/mupdf/lib/*.wasm"],
Expand Down
Loading