From 6763e9454e82f22e677339e093f9053e4e3246fa Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 28 Jan 2024 07:00:29 -0800 Subject: [PATCH] Optionally use local instance of impress-2020 during development To activate this, I created a `.env.development` file in my project root, with the following content: ```env IMPRESS_2020_ORIGIN=http://localhost:4000 ``` Then, I started impress-2020 with `yarn dev --port=4000`. Now, the app loads from there, hooray!! It even fixes that obnoxious pet state ID bug that happens when you run against the production db lol --- app/helpers/application_helper.rb | 5 +++++ .../wardrobe-2020/WardrobePage/useOutfitState.js | 2 +- app/javascript/wardrobe-2020/apolloClient.js | 4 +++- .../wardrobe-2020/components/SpeciesColorPicker.js | 3 ++- app/javascript/wardrobe-2020/impress-2020-config.js | 10 ++++++++++ app/javascript/wardrobe-2020/util.js | 6 ++++-- app/views/layouts/application.html.haml | 1 + app/views/outfits/edit.html.haml | 1 + config/initializers/impress_2020.rb | 2 ++ config/routes.rb | 3 ++- 10 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 app/javascript/wardrobe-2020/impress-2020-config.js create mode 100644 config/initializers/impress_2020.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fb9daa65..8497fa28 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -87,6 +87,11 @@ module ApplicationHelper !@hide_home_link end + def impress_2020_meta_tag + tag 'meta', name: "impress-2020-origin", + content: Rails.configuration.impress_2020_origin + end + JAVASCRIPT_LIBRARIES = { :jquery => 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js', :jquery20 => 'https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js', diff --git a/app/javascript/wardrobe-2020/WardrobePage/useOutfitState.js b/app/javascript/wardrobe-2020/WardrobePage/useOutfitState.js index 6308cbb3..5c4073cd 100644 --- a/app/javascript/wardrobe-2020/WardrobePage/useOutfitState.js +++ b/app/javascript/wardrobe-2020/WardrobePage/useOutfitState.js @@ -629,7 +629,7 @@ export function buildOutfitUrl(outfitState, options = {}) { const origin = typeof window !== "undefined" ? window.location.origin - : "https://impress-2020.openneo.net"; + : "https://impress.openneo.net"; return origin + buildOutfitPath(outfitState, options); } diff --git a/app/javascript/wardrobe-2020/apolloClient.js b/app/javascript/wardrobe-2020/apolloClient.js index d53aa033..e3116a41 100644 --- a/app/javascript/wardrobe-2020/apolloClient.js +++ b/app/javascript/wardrobe-2020/apolloClient.js @@ -2,6 +2,8 @@ import { ApolloClient, createHttpLink, InMemoryCache } from "@apollo/client"; import { loadErrorMessages, loadDevMessages } from "@apollo/client/dev"; import { createPersistedQueryLink } from "apollo-link-persisted-queries"; +import { buildImpress2020Url } from "./impress-2020-config"; + // Use Apollo's error messages in development. if (process.env["NODE_ENV"] === "development") { loadErrorMessages(); @@ -159,7 +161,7 @@ const typePolicies = { const cache = new InMemoryCache({ typePolicies }); const httpLink = createHttpLink({ - uri: "https://impress-2020.openneo.net/api/graphql", + uri: buildImpress2020Url("/api/graphql"), }); const link = createPersistedQueryLink({ diff --git a/app/javascript/wardrobe-2020/components/SpeciesColorPicker.js b/app/javascript/wardrobe-2020/components/SpeciesColorPicker.js index 2adfb438..88cffafb 100644 --- a/app/javascript/wardrobe-2020/components/SpeciesColorPicker.js +++ b/app/javascript/wardrobe-2020/components/SpeciesColorPicker.js @@ -3,6 +3,7 @@ import gql from "graphql-tag"; import { useQuery } from "@apollo/client"; import { Box, Flex, Select, Text, useColorModeValue } from "@chakra-ui/react"; +import { buildImpress2020Url } from "../impress-2020-config"; import { Delay, logAndCapture, useFetch } from "../util"; /** @@ -346,7 +347,7 @@ let cachedResponseForAllValidPetPoses = null; */ export function useAllValidPetPoses() { const networkResponse = useFetch( - "https://impress-2020.openneo.net/api/validPetPoses", + buildImpress2020Url("/api/validPetPoses"), { responseType: "arrayBuffer", // If we already have globally-cached valids, skip the request. diff --git a/app/javascript/wardrobe-2020/impress-2020-config.js b/app/javascript/wardrobe-2020/impress-2020-config.js new file mode 100644 index 00000000..1cae15f9 --- /dev/null +++ b/app/javascript/wardrobe-2020/impress-2020-config.js @@ -0,0 +1,10 @@ +const IMPRESS_2020_ORIGIN = readImpress2020Origin(); + +export function buildImpress2020Url(path) { + return new URL(path, IMPRESS_2020_ORIGIN).toString(); +} + +function readImpress2020Origin() { + const node = document.querySelector("meta[name=impress-2020-origin]"); + return node?.content || "https://impress-2020.openneo.net" +} diff --git a/app/javascript/wardrobe-2020/util.js b/app/javascript/wardrobe-2020/util.js index c48f3602..f99b7f11 100644 --- a/app/javascript/wardrobe-2020/util.js +++ b/app/javascript/wardrobe-2020/util.js @@ -11,6 +11,8 @@ import loadableLibrary from "@loadable/component"; import * as Sentry from "@sentry/react"; import { WarningIcon } from "@chakra-ui/icons"; +import { buildImpress2020Url } from "./impress-2020-config"; + import ErrorGrundoImg from "./images/error-grundo.png"; import ErrorGrundoImg2x from "./images/error-grundo@2x.png"; @@ -141,7 +143,7 @@ export function safeImageUrl( `safeImageUrl could not parse URL: ${urlString}. Returning a placeholder.`, ), ); - return "https://impress-2020.openneo.net/__error__URL-was-not-parseable__"; + return buildImpress2020Url("/__error__URL-was-not-parseable__"); } // Rewrite Neopets URLs to their HTTPS equivalents, and additionally to our @@ -178,7 +180,7 @@ export function safeImageUrl( `upgrade it to HTTPS: ${urlString}. Returning a placeholder.`, ), ); - return "https://impress-2020.openneo.net/__error__URL-was-not-HTTPS__"; + return buildImpress2020Url("/__error__URL-was-not-HTTPS__"); } return url.toString(); diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index acbfd9c1..b461b4cb 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -17,6 +17,7 @@ = yield :meta = open_graph_tags = csrf_meta_tag + = impress_2020_meta_tag = signed_in_meta_tag - if user_signed_in? = current_user_id_meta_tag diff --git a/app/views/outfits/edit.html.haml b/app/views/outfits/edit.html.haml index bad166dc..f7b8608c 100644 --- a/app/views/outfits/edit.html.haml +++ b/app/views/outfits/edit.html.haml @@ -20,6 +20,7 @@ = render 'static/analytics' = open_graph_tags = csrf_meta_tags + = impress_2020_meta_tag %meta{name: 'dti-current-user-id', content: user_signed_in? ? current_user.id : "null"} %body #wardrobe-2020-root diff --git a/config/initializers/impress_2020.rb b/config/initializers/impress_2020.rb new file mode 100644 index 00000000..0f93a47a --- /dev/null +++ b/config/initializers/impress_2020.rb @@ -0,0 +1,2 @@ +Rails.configuration.impress_2020_origin = + ENV["IMPRESS_2020_ORIGIN"] || "https://impress-2020.openneo.net" diff --git a/config/routes.rb b/config/routes.rb index e9c264cc..99912fb3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,7 +87,8 @@ OpenneoImpressItems::Application.routes.draw do get '/donate' => 'campaigns#current', as: :donate # Static pages! - get '/terms' => redirect("https://impress-2020.openneo.net/terms"), :as => :terms + get '/terms', as: :terms, + to: redirect(Rails.configuration.impress_2020_origin + "/terms") # Other useful lil things! get '/sitemap.xml' => 'sitemap#index', :as => :sitemap, :format => :xml