diff --git a/enterprise/coderd/coderd.go b/enterprise/coderd/coderd.go index 0021fd4df63c4..2cc54bd3fa705 100644 --- a/enterprise/coderd/coderd.go +++ b/enterprise/coderd/coderd.go @@ -67,6 +67,7 @@ func New(ctx context.Context, options *Options) (_ *API, err error) { api.AGPL.Options.SetUserGroups = api.setUserGroups api.AGPL.SiteHandler.AppearanceFetcher = api.fetchAppearanceConfig + api.AGPL.SiteHandler.RegionsFetcher = api.fetchRegions oauthConfigs := &httpmw.OAuth2Configs{ Github: options.GithubOAuth2Config, diff --git a/enterprise/coderd/workspaceproxy.go b/enterprise/coderd/workspaceproxy.go index 8da03397b4e6e..296c6482be2a5 100644 --- a/enterprise/coderd/workspaceproxy.go +++ b/enterprise/coderd/workspaceproxy.go @@ -39,23 +39,30 @@ func (api *API) forceWorkspaceProxyHealthUpdate(ctx context.Context) { // NOTE: this doesn't need a swagger definition since AGPL already has one, and // this route overrides the AGPL one. func (api *API) regions(rw http.ResponseWriter, r *http.Request) { - ctx := r.Context() - //nolint:gocritic // this route intentionally requests resources that users + regions, err := api.fetchRegions(r.Context()) + if err != nil { + httpapi.InternalServerError(rw, err) + return + } + + httpapi.Write(r.Context(), rw, http.StatusOK, regions) +} + +func (api *API) fetchRegions(ctx context.Context) (codersdk.RegionsResponse, error) { + //nolint:gocritic // this intentionally requests resources that users // cannot usually access in order to give them a full list of available // regions. ctx = dbauthz.AsSystemRestricted(ctx) primaryRegion, err := api.AGPL.PrimaryRegion(ctx) if err != nil { - httpapi.InternalServerError(rw, err) - return + return codersdk.RegionsResponse{}, err } regions := []codersdk.Region{primaryRegion} proxies, err := api.Database.GetWorkspaceProxies(ctx) if err != nil { - httpapi.InternalServerError(rw, err) - return + return codersdk.RegionsResponse{}, err } // Only add additional regions if the proxy health is enabled. @@ -81,9 +88,9 @@ func (api *API) regions(rw http.ResponseWriter, r *http.Request) { } } - httpapi.Write(ctx, rw, http.StatusOK, codersdk.RegionsResponse{ + return codersdk.RegionsResponse{ Regions: regions, - }) + }, nil } // @Summary Update workspace proxy diff --git a/site/index.html b/site/index.html index 61cb49be7b129..ac4525c1aa9e6 100644 --- a/site/index.html +++ b/site/index.html @@ -20,6 +20,7 @@ + = ({ children }) => { ) const queryKey = ["get-proxies"] + // This doesn't seem like an idiomatic way to get react-query to use the + // initial data without performing an API request on mount, but it works. + // + // If anyone would like to clean this up in the future, it should seed data + // from the `meta` tag if it exists, and not fetch the regions route. + const [initialData] = useState(() => { + // Build info is injected by the Coder server into the HTML document. + const regions = document.querySelector("meta[property=regions]") + if (regions) { + const rawContent = regions.getAttribute("content") + try { + return JSON.parse(rawContent as string) + } catch (ex) { + // Ignore this and fetch as normal! + } + } + }) const { data: proxiesResp, error: proxiesError, @@ -100,6 +117,8 @@ export const ProxyProvider: FC = ({ children }) => { } = useQuery({ queryKey, queryFn: getWorkspaceProxies, + staleTime: initialData ? Infinity : undefined, + initialData, }) // Every time we get a new proxiesResponse, update the latency check