Skip to content

Commit 30e5673

Browse files
sshaderConvex, Inc.
authored and
Convex, Inc.
committed
Fix a couple anonymous dev related bugs (#36207)
* My port picking logic was slightly wrong -- if I told it to pick ports starting at 1234, it would actually start at 1235. * The logic for parsing query params for the self hosted dashboard wasn't getting hit when serving the static export. I moved this into an effect in the render path instead. GitOrigin-RevId: 1528728dd2f9aee4920866af5272f1f183cc0a55
1 parent b19e1ca commit 30e5673

File tree

2 files changed

+39
-27
lines changed
  • npm-packages
    • convex/src/cli/lib/localDeployment
    • dashboard-self-hosted/src/pages

2 files changed

+39
-27
lines changed

npm-packages/convex/src/cli/lib/localDeployment/utils.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export async function choosePorts(
2929
}
3030
ports.push(port);
3131
} else {
32-
const lastPort = ports.length > 0 ? ports[ports.length - 1] : startPort;
33-
const port = await detect(lastPort + 1);
32+
const portToTry =
33+
ports.length > 0 ? ports[ports.length - 1] + 1 : startPort;
34+
const port = await detect(portToTry);
3435
ports.push(port);
3536
}
3637
}

npm-packages/dashboard-self-hosted/src/pages/_app.tsx

+36-25
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ function App({
3434
pageProps: {
3535
deploymentUrl,
3636
adminKey,
37-
listDeploymentsApiUrl,
38-
selectedDeploymentName,
37+
defaultListDeploymentsApiUrl,
3938
...pageProps
4039
},
4140
}: AppProps & {
4241
pageProps: {
4342
deploymentUrl: string | null;
4443
adminKey: string | null;
45-
listDeploymentsApiUrl: string | null;
46-
selectedDeploymentName: string | null;
44+
defaultListDeploymentsApiUrl: string | null;
4745
};
4846
}) {
4947
return (
@@ -60,8 +58,7 @@ function App({
6058
<DeploymentInfoProvider
6159
deploymentUrl={deploymentUrl}
6260
adminKey={adminKey}
63-
listDeploymentsApiUrl={listDeploymentsApiUrl}
64-
selectedDeploymentName={selectedDeploymentName}
61+
defaultListDeploymentsApiUrl={defaultListDeploymentsApiUrl}
6562
>
6663
<DeploymentApiProvider deploymentOverride="local">
6764
<WaitForDeploymentApi>
@@ -97,16 +94,15 @@ function normalizeUrl(url: string) {
9794
App.getInitialProps = async ({ ctx }: { ctx: { req?: any } }) => {
9895
// On server-side, get from process.env
9996
if (ctx.req) {
100-
// This is a relative URL, so add localhost as the origin so it can be parsed
101-
const url = new URL(ctx.req.url, "http://127.0.0.1");
97+
// Note -- we can't use `ctx.req.url` when serving the dashboard statically,
98+
// so instead we'll read from query params on the client side.
10299

103100
let deploymentUrl: string | null = null;
104101
if (process.env.NEXT_PUBLIC_DEPLOYMENT_URL) {
105102
deploymentUrl = normalizeUrl(process.env.NEXT_PUBLIC_DEPLOYMENT_URL);
106103
}
107104

108105
const listDeploymentsApiPort =
109-
url.searchParams.get(LIST_DEPLOYMENTS_API_PORT_QUERY_PARAM) ??
110106
process.env.NEXT_PUBLIC_DEFAULT_LIST_DEPLOYMENTS_API_PORT;
111107
let listDeploymentsApiUrl: string | null = null;
112108
if (listDeploymentsApiPort) {
@@ -116,15 +112,11 @@ App.getInitialProps = async ({ ctx }: { ctx: { req?: any } }) => {
116112
}
117113
}
118114

119-
const selectedDeploymentName =
120-
url.searchParams.get(SELECTED_DEPLOYMENT_NAME_QUERY_PARAM) ?? null;
121-
122115
return {
123116
pageProps: {
124117
deploymentUrl,
125118
adminKey: null,
126-
listDeploymentsApiUrl,
127-
selectedDeploymentName,
119+
defaultListDeploymentsApiUrl: listDeploymentsApiUrl,
128120
},
129121
};
130122
}
@@ -142,7 +134,7 @@ App.getInitialProps = async ({ ctx }: { ctx: { req?: any } }) => {
142134
pageProps: {
143135
deploymentUrl: clientSideDeploymentUrl ?? null,
144136
adminKey: clientSideAdminKey ?? null,
145-
listDeploymentsApiUrl: clientSideListDeploymentsApiUrl ?? null,
137+
defaultListDeploymentsApiUrl: clientSideListDeploymentsApiUrl ?? null,
146138
selectedDeploymentName: clientSideSelectedDeploymentName ?? null,
147139
},
148140
};
@@ -227,18 +219,19 @@ function DeploymentInfoProvider({
227219
children,
228220
deploymentUrl,
229221
adminKey,
230-
listDeploymentsApiUrl,
231-
selectedDeploymentName,
222+
defaultListDeploymentsApiUrl,
232223
}: {
233224
children: React.ReactNode;
234225
deploymentUrl: string | null;
235226
adminKey: string | null;
236-
listDeploymentsApiUrl: string | null;
237-
selectedDeploymentName: string | null;
227+
defaultListDeploymentsApiUrl: string | null;
238228
}) {
239-
const [shouldListDeployments, setShouldListDeployments] = useState(
240-
listDeploymentsApiUrl !== null,
241-
);
229+
const [listDeploymentsApiUrl, setListDeploymentsApiUrl] = useState<
230+
string | null
231+
>(null);
232+
const [selectedDeploymentName, setSelectedDeploymentName] = useState<
233+
string | null
234+
>(null);
242235
const [isValidDeploymentInfo, setIsValidDeploymentInfo] = useState<
243236
boolean | null
244237
>(null);
@@ -297,22 +290,40 @@ function DeploymentInfoProvider({
297290
useEffect(() => {
298291
if (typeof window !== "undefined") {
299292
const url = new URL(window.location.href);
293+
const listDeploymentsApiPort = url.searchParams.get(
294+
LIST_DEPLOYMENTS_API_PORT_QUERY_PARAM,
295+
);
296+
const selectedDeploymentNameFromUrl = url.searchParams.get(
297+
SELECTED_DEPLOYMENT_NAME_QUERY_PARAM,
298+
);
300299
url.searchParams.delete(LIST_DEPLOYMENTS_API_PORT_QUERY_PARAM);
301300
url.searchParams.delete(SELECTED_DEPLOYMENT_NAME_QUERY_PARAM);
302301
window.history.replaceState({}, "", url.toString());
302+
if (listDeploymentsApiPort) {
303+
if (!Number.isNaN(parseInt(listDeploymentsApiPort))) {
304+
setListDeploymentsApiUrl(
305+
normalizeUrl(`http://127.0.0.1:${listDeploymentsApiPort}`),
306+
);
307+
}
308+
} else {
309+
setListDeploymentsApiUrl(defaultListDeploymentsApiUrl);
310+
}
311+
if (selectedDeploymentNameFromUrl) {
312+
setSelectedDeploymentName(selectedDeploymentNameFromUrl);
313+
}
303314
}
304-
}, []);
315+
}, [defaultListDeploymentsApiUrl]);
305316
if (!mounted) return null;
306317

307318
if (!isValidDeploymentInfo) {
308319
return (
309320
<div className="flex h-screen w-screen flex-col items-center justify-center gap-8">
310321
<ConvexLogo />
311-
{shouldListDeployments && listDeploymentsApiUrl !== null ? (
322+
{listDeploymentsApiUrl !== null ? (
312323
<DeploymentList
313324
listDeploymentsApiUrl={listDeploymentsApiUrl}
314325
onError={() => {
315-
setShouldListDeployments(false);
326+
setListDeploymentsApiUrl(null);
316327
}}
317328
onSelect={onSubmit}
318329
selectedDeploymentName={selectedDeploymentName}

0 commit comments

Comments
 (0)