Skip to content

Commit

Permalink
ENH Allow base URL to not have trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jan 17, 2023
1 parent 729e545 commit 1d3eced
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 17 deletions.
4 changes: 2 additions & 2 deletions client/dist/js/bundle.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion client/src/boot/BootRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import i18n from 'i18n';
import { isDirty } from 'redux-form';
import getFormState from 'lib/getFormState';
import { Routes, Route } from 'react-router';
import { joinUrlPaths } from 'lib/urls';

/**
* Bootstraps routes
Expand Down Expand Up @@ -115,7 +116,7 @@ class BootRoutes {
ReactDOM.createRoot(document.getElementsByClassName('cms-content')[0]).render(
<ApolloProvider client={this.client}>
<ReduxProvider store={this.store}>
<BrowserRouter basename={`${Config.get('baseUrl')}${Config.get('adminUrl')}`}>
<BrowserRouter basename={joinUrlPaths(Config.get('baseUrl'), Config.get('adminUrl'))}>
<Routes>
<Route path={rootRoute.path} element={<rootRoute.component />}>{routes}</Route>
</Routes>
Expand Down
3 changes: 2 additions & 1 deletion client/src/boot/apollo/buildNetworkComponents.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ApolloLink, HttpLink } from '@apollo/client';
import { onError } from '@apollo/client/link/error';
import Config from 'lib/Config';
import { joinUrlPaths } from 'lib/urls';

const buildNetworkComponents = (baseUrl) => {
const httpLink = new HttpLink({
uri: `${baseUrl}admin/graphql`,
uri: joinUrlPaths(baseUrl, 'admin/graphql'),
fetchOptions: {
credentials: 'same-origin',
headers: {
Expand Down
11 changes: 6 additions & 5 deletions client/src/boot/apollo/getGraphqlFragments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetch from 'isomorphic-fetch';
import { joinUrlPaths } from 'lib/urls';

const parseResponse = result => {
const fragmentData = result.data;
Expand All @@ -25,13 +26,13 @@ const getGraphqlFragments = async (baseUrl, preferStatic = true) => {
const isLegacy = !!document.body.getAttribute('data-graphql-legacy');

const urls = [
`${baseUrl}_graphql/admin.types.graphql`,
`${baseUrl}admin.types.graphql`,
joinUrlPaths(baseUrl, '_graphql/admin.types.graphql'),
joinUrlPaths(baseUrl, 'admin.types.graphql'),
];

const legacyURLs = [
`${baseUrl}admin/graphql/types`,
`${baseUrl}assets/admin.types.graphql`,
joinUrlPaths(baseUrl, 'admin/graphql/types'),
joinUrlPaths(baseUrl, 'assets/admin.types.graphql'),
];

let primaryURL;
Expand All @@ -51,7 +52,7 @@ const getGraphqlFragments = async (baseUrl, preferStatic = true) => {
headers: {
'Content-Type': 'application/json'
},
uri: `${baseUrl}`,
uri: baseUrl,
credentials: 'same-origin',
};

Expand Down
1 change: 1 addition & 0 deletions client/src/bundles/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import 'expose-loader?exposes=ShortcodeSerialiser!lib/ShortcodeSerialiser';
import 'expose-loader?exposes=formatWrittenNumber!lib/formatWrittenNumber';
import 'expose-loader?exposes=withDragDropContext!lib/withDragDropContext';
import 'expose-loader?exposes=withRouter!lib/withRouter';
import 'expose-loader?exposes=ssUrlLib!lib/urls';

// Legacy CMS
import '../legacy/jquery.changetracker';
Expand Down
3 changes: 2 additions & 1 deletion client/src/legacy/LeftAndMain.Menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import $ from 'jquery';
import { joinUrlPaths } from 'lib/urls';

$.entwine('ss', function($){

Expand Down Expand Up @@ -296,7 +297,7 @@ $.entwine('ss', function($){
var item = this.getMenuItem();

var url = this.attr('href');
if(!isExternal) url = $('base').attr('href') + url;
if(!isExternal) url = joinUrlPaths($('base').attr('href'), url);

var children = item.find('li');
if(children.length) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/legacy/LeftAndMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ window.ss.tabStateUrl = function() {
return window.location.href
.replace(/\?.*/, '')
.replace(/#.*/, '')
.replace($('base').attr('href'), '');
.replace(new RegExp(`^${$('base').attr('href')}/?`), '');
},

$(window).on('resize.leftandmain', function(e) {
Expand Down
5 changes: 3 additions & 2 deletions client/src/lib/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ function resolveURLToBase(path) {
return absolutePath;
}

// Remove base url from absolute path, save for trailing `/` which Page.js requires
return absolutePath.substring(absoluteBase.length - 1);
// Remove base url from absolute path, save for leading `/` which Page.js requires
const regex = new RegExp(`^${absoluteBase}/?`);
return absolutePath.replace(regex, '/');
}

/**
Expand Down
8 changes: 8 additions & 0 deletions client/src/lib/urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const joinUrlPaths = (...links) => {
let finalLink = links.shift();
// eslint-disable-next-line no-restricted-syntax
for (const link of links) {
finalLink = `${finalLink.replace(/\/$/, '')}/${link.replace(/^\//, '')}`;
}
return finalLink;
};
2 changes: 1 addition & 1 deletion code/CMSMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected static function menuitem_for_controller($controllerClass)
return null;
}

$link = Controller::join_links($urlBase, $urlSegment) . '/';
$link = Controller::join_links($urlBase, $urlSegment);

// doesn't work if called outside of a controller context (e.g. in _config.php)
// as the locale won't be detected properly. Use {@link LeftAndMain->MainMenu()} to update
Expand Down
3 changes: 1 addition & 2 deletions code/LeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,6 @@ public function Link($action = null)
$link = Controller::join_links(
AdminRootController::admin_url(),
$segment,
'/', // trailing slash needed if $action is null!
"$action"
);
$this->extend('updateLink', $link);
Expand Down Expand Up @@ -1664,7 +1663,7 @@ public function currentPageID()
if (is_numeric($this->getRequest()->param('ID'))) {
return $this->getRequest()->param('ID');
}

/** @deprecated */
$session = $this->getRequest()->getSession();
return $session->get($this->sessionNamespace() . ".currentPage") ?: null;
Expand Down
3 changes: 2 additions & 1 deletion tests/php/LeftAndMainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\Admin\CMSMenu;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Assets\File;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Manifest\ModuleLoader;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function testLeftAndMainSubclasses()
$this->assertGreaterThan(0, count($menuItems ?? []));

$adminUrl = AdminRootController::admin_url();
$menuItem = $menuItems->find('Link', $adminUrl . 'security/');
$menuItem = $menuItems->find('Link', Controller::join_links($adminUrl, 'security/'));
$this->assertNotEmpty($menuItem, 'Security not found in the menu items list');

$link = $menuItem->Link;
Expand Down

0 comments on commit 1d3eced

Please sign in to comment.