Skip to content

Commit

Permalink
Drop hash router (#177)
Browse files Browse the repository at this point in the history
Switching from HashRouter to BrowserRouter.
This will switch urls from like
https://near.social/#/mob.near/widget/Welcome to
https://near.social/mob.near/widget/Welcome and automatically redirect.
  • Loading branch information
Evgeny Kuzyakov authored Jul 28, 2023
1 parent db725d8 commit 5ff6b26
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# production
/build
/dist

# misc
.DS_Store
Expand Down
3 changes: 3 additions & 0 deletions config/webpack.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ module.exports = () => ({
static: path.resolve(__dirname, "../dist"),
port: 3000,
compress: true,
historyApiFallback: {
disableDotRule: true,
},
},
plugins: [new HotModuleReplacementPlugin()],
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frontend",
"version": "0.12.3",
"version": "0.13.0",
"homepage": "/",
"private": true,
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="favicon.png" />
<link rel="icon" href="/favicon.png" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
Expand All @@ -16,12 +16,12 @@
<meta property="og:type" content="website">
<meta property="og:title" content="Near Social" />
<meta property="og:description" content="Social data protocol built on NEAR" />
<link rel="apple-touch-icon" href="favicon.png" />
<link rel="apple-touch-icon" href="/favicon.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="manifest.json" />
<link rel="manifest" href="/manifest.json" />
<title>Near Social</title>
</head>
<body>
Expand Down
5 changes: 2 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "react-bootstrap-typeahead/css/Typeahead.css";
import "react-bootstrap-typeahead/css/Typeahead.bs5.css";
import "bootstrap/dist/js/bootstrap.bundle";
import "App.scss";
import { HashRouter as Router, Link, Route, Switch } from "react-router-dom";
import { BrowserRouter as Router, Link, Route, Switch } from "react-router-dom";
import EditorPage from "./pages/EditorPage";
import ViewPage from "./pages/ViewPage";
import { setupWalletSelector } from "@near-wallet-selector/core";
Expand Down Expand Up @@ -47,9 +47,8 @@ function App(props) {
const { initNear } = useInitNear();
const near = useNear();
const account = useAccount();
const accountId = account.accountId;

const location = window.location;
const accountId = account.accountId;

useEffect(() => {
initNear &&
Expand Down
37 changes: 37 additions & 0 deletions src/hooks/useHashRouterLegacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useHistory } from "react-router-dom";
import React, { useCallback, useEffect } from "react";

export function useHashRouterLegacy() {
const history = useHistory();

const onHashChange = useCallback(
(event) => {
let url = event.newURL.split("#").pop() ?? "/";

if (url[0] === "/") {
history && history.replace(url);
}
},
[history]
);

useEffect(() => {
window.addEventListener("hashchange", onHashChange);

return () => {
window.removeEventListener("hashchange", onHashChange);
};
}, [onHashChange]);

useEffect(() => {
if (!history) {
return;
}
const currentUrl = window.location.href;

if (currentUrl.includes("#")) {
const path = currentUrl.split("#")[1];
history.replace(path);
}
}, [history]);
}
7 changes: 0 additions & 7 deletions src/hooks/useQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,5 @@ import React from "react";

export function useQuery() {
let { search } = useLocation();
if (location.search) {
if (!search) {
search = location.search;
} else {
search += `&${location.search.substring(1)}`;
}
}
return React.useMemo(() => new URLSearchParams(search), [search]);
}
4 changes: 3 additions & 1 deletion src/pages/EditorPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
StorageType,
toPath,
} from "../components/Editor/FileTab";
import { useHashRouterLegacy } from "../hooks/useHashRouterLegacy";

const LsKey = "social.near:v01:";
const EditorLayoutKey = LsKey + "editorLayout:";
Expand All @@ -41,6 +42,7 @@ const Layout = {
};

export default function EditorPage(props) {
useHashRouterLegacy();
const { widgetSrc } = useParams();
const history = useHistory();
const setWidgetSrc = props.setWidgetSrc;
Expand Down Expand Up @@ -595,7 +597,7 @@ export default function EditorPage(props) {
{path && accountId && (
<a
className="btn btn-outline-primary"
href={`#/${widgetPath}`}
href={`/${widgetPath}`}
target="_blank"
rel="noopener noreferrer"
>
Expand Down
4 changes: 3 additions & 1 deletion src/pages/EmbedPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React, { useEffect, useState } from "react";
import { Widget } from "near-social-vm";
import { useParams } from "react-router-dom";
import { useQuery } from "../hooks/useQuery";
import { useHashRouterLegacy } from "../hooks/useHashRouterLegacy";

export default function EmbedPage(props) {
useHashRouterLegacy();

const { widgetSrc } = useParams();
const query = useQuery();
const [widgetProps, setWidgetProps] = useState({});

const src = widgetSrc || props.widgets.default;

useEffect(() => {
Expand Down
3 changes: 3 additions & 0 deletions src/pages/ViewPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import React, { useEffect, useState } from "react";
import { Widget } from "near-social-vm";
import { useParams } from "react-router-dom";
import { useQuery } from "../hooks/useQuery";
import { useHashRouterLegacy } from "../hooks/useHashRouterLegacy";

export default function ViewPage(props) {
useHashRouterLegacy();

const { widgetSrc } = useParams();
const query = useQuery();
const [widgetProps, setWidgetProps] = useState({});
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const HTMLWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { merge } = require("webpack-merge");
const loadPreset = require("./config/presets/loadPreset");
const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
const loadConfig = (mode) => require(`./config/webpack.${mode}.js`)(mode);

module.exports = function (env) {
Expand Down Expand Up @@ -83,6 +82,7 @@ module.exports = function (env) {
template: `${paths.publicPath}/index.html`,
favicon: `${paths.publicPath}/favicon.png`,
robots: `${paths.publicPath}/robots.txt`,
publicPath: "/",
}),
new webpack.ProgressPlugin(),
new webpack.ProvidePlugin({
Expand Down

0 comments on commit 5ff6b26

Please sign in to comment.