mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
load custom js when the app is done loading to ease custom code usage (#1093)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { i18n } from "@lingui/core"
|
import { i18n } from "@lingui/core"
|
||||||
import { I18nProvider } from "@lingui/react"
|
import { I18nProvider } from "@lingui/react"
|
||||||
import { MantineProvider } from "@mantine/core"
|
import { MantineProvider } from "@mantine/core"
|
||||||
|
import { useDidUpdate } from "@mantine/hooks"
|
||||||
import { ModalsProvider } from "@mantine/modals"
|
import { ModalsProvider } from "@mantine/modals"
|
||||||
import { Notifications } from "@mantine/notifications"
|
import { Notifications } from "@mantine/notifications"
|
||||||
import { Constants } from "app/constants"
|
import { Constants } from "app/constants"
|
||||||
@@ -28,7 +29,7 @@ import { LoginPage } from "pages/auth/LoginPage"
|
|||||||
import { PasswordRecoveryPage } from "pages/auth/PasswordRecoveryPage"
|
import { PasswordRecoveryPage } from "pages/auth/PasswordRecoveryPage"
|
||||||
import { RegistrationPage } from "pages/auth/RegistrationPage"
|
import { RegistrationPage } from "pages/auth/RegistrationPage"
|
||||||
import { WelcomePage } from "pages/WelcomePage"
|
import { WelcomePage } from "pages/WelcomePage"
|
||||||
import React, { useEffect } from "react"
|
import React, { useEffect, useRef } from "react"
|
||||||
import ReactGA from "react-ga4"
|
import ReactGA from "react-ga4"
|
||||||
import { HashRouter, Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom"
|
import { HashRouter, Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom"
|
||||||
import Tinycon from "tinycon"
|
import Tinycon from "tinycon"
|
||||||
@@ -164,6 +165,40 @@ function BrowserExtensionBadgeUnreadCountHandler() {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function CustomJs() {
|
||||||
|
const scriptLoaded = useRef(false)
|
||||||
|
|
||||||
|
// useDidUpdate is used instead of useEffect because we want to skip the first render
|
||||||
|
// the first render is the render of react-router, the routes are actually loaded in a second render
|
||||||
|
// we want the script to be executed when the first route is done loading
|
||||||
|
useDidUpdate(() => {
|
||||||
|
if (scriptLoaded.current) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const script = document.createElement("script")
|
||||||
|
script.src = "custom_js.js"
|
||||||
|
script.async = true
|
||||||
|
document.body.appendChild(script)
|
||||||
|
|
||||||
|
scriptLoaded.current = true
|
||||||
|
})
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
function CustomCss() {
|
||||||
|
useEffect(() => {
|
||||||
|
const link = document.createElement("link")
|
||||||
|
link.rel = "stylesheet"
|
||||||
|
link.type = "text/css"
|
||||||
|
link.href = "custom_css.css"
|
||||||
|
document.head.appendChild(link)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
export function App() {
|
export function App() {
|
||||||
useI18n()
|
useI18n()
|
||||||
const dispatch = useAppDispatch()
|
const dispatch = useAppDispatch()
|
||||||
@@ -181,6 +216,8 @@ export function App() {
|
|||||||
<GoogleAnalyticsHandler />
|
<GoogleAnalyticsHandler />
|
||||||
<RedirectHandler />
|
<RedirectHandler />
|
||||||
<AppRoutes />
|
<AppRoutes />
|
||||||
|
<CustomJs />
|
||||||
|
<CustomCss />
|
||||||
</HashRouter>
|
</HashRouter>
|
||||||
</>
|
</>
|
||||||
</Providers>
|
</Providers>
|
||||||
|
|||||||
@@ -1,41 +1,13 @@
|
|||||||
import { lingui } from "@lingui/vite-plugin"
|
import { lingui } from "@lingui/vite-plugin"
|
||||||
import react from "@vitejs/plugin-react"
|
import react from "@vitejs/plugin-react"
|
||||||
import { visualizer } from "rollup-plugin-visualizer"
|
import { visualizer } from "rollup-plugin-visualizer"
|
||||||
import { defineConfig, PluginOption } from "vite"
|
import { defineConfig } from "vite"
|
||||||
import eslint from "vite-plugin-eslint"
|
import eslint from "vite-plugin-eslint"
|
||||||
import tsconfigPaths from "vite-tsconfig-paths"
|
import tsconfigPaths from "vite-tsconfig-paths"
|
||||||
|
|
||||||
// inject custom js and css links in html
|
|
||||||
const customCodeInjector: PluginOption = {
|
|
||||||
name: "customCodeInjector",
|
|
||||||
transformIndexHtml: html => {
|
|
||||||
return {
|
|
||||||
html,
|
|
||||||
tags: [
|
|
||||||
{
|
|
||||||
tag: "script",
|
|
||||||
attrs: {
|
|
||||||
src: "custom_js.js",
|
|
||||||
},
|
|
||||||
injectTo: "body",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag: "link",
|
|
||||||
attrs: {
|
|
||||||
rel: "stylesheet",
|
|
||||||
href: "custom_css.css",
|
|
||||||
},
|
|
||||||
injectTo: "head",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
// https://vitejs.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [
|
plugins: [
|
||||||
customCodeInjector,
|
|
||||||
react({
|
react({
|
||||||
babel: {
|
babel: {
|
||||||
// babel-macro is needed for lingui
|
// babel-macro is needed for lingui
|
||||||
|
|||||||
Reference in New Issue
Block a user