Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix initial page routeId and improve router handling
Assigns routeId to the initial page element in React, Svelte, and Vue view components if available. Fixes a typo in get-router-initial-component.js for all frameworks. Improves router-class.js to handle /index.html more robustly and adds a warning when the initial page is not found, prompting the user to set the data-url attribute.
  • Loading branch information
huajiqaq committed Jan 24, 2026
commit af19da86c0a2fe6dbfa566d7e76e0607657aa97a
16 changes: 12 additions & 4 deletions src/core/modules/router/router-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,9 +913,13 @@ class Router extends Framework7Class {
);
browserHistoryRoot = location.pathname.split('index.html')[0];
} else if (browserHistory && !browserHistoryRoot) {
if (documentUrl.includes('index.html')) {
history.replaceState(history.state, document.title, location.pathname.replace(/\/index\.html/gi, '/') + location.search + location.hash);
documentUrl = documentUrl.replace(/\/index\.html/gi, '/');
if (documentUrl.startsWith('/index.html')) {
window.history.replaceState(
window.history.state,
document.title,
location.pathname.replace(/^\/index\.html/, '/') + location.search + location.hash
);
documentUrl = documentUrl.replace(/^\/index\.html/, '/');
}
}
if (!browserHistory || !browserHistoryOnLoad) {
Expand Down Expand Up @@ -947,7 +951,11 @@ class Router extends Framework7Class {
const initialRoute = router.findMatchingRoute(initialUrl);
const anotherViewName = initialRoute.route.viewName;
if (initialRoute && anotherViewName && app.view[anotherViewName] !== view) {
initialUrl = router.params.url || documentUrl.split(browserHistorySeparator)[0] || '/';
if (router.params.url) {
initialUrl = router.params.url;
} else {
console.error('Framework7: Initial page not found, using default URL (null). Please set the data-url attribute on your view element.', view);
}
}

if (router.history.indexOf(initialUrl) >= 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/react/components/view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ const View = (props) => {
f7View.current.init(elRef.current);
if (initialPage) {
initialPage.el = f7View.current.router.currentPageEl;
const routeId = pages?.[0]?.props?.routeId;
if (routeId) initialPage.el.routeId = routeId;
if (initialRoute && initialRoute.route && initialRoute.route.keepAlive) {
initialRoute.route.keepAliveData = { pageEl: initialPage.el };
}
Expand All @@ -192,6 +194,8 @@ const View = (props) => {
f7View.current.init(elRef.current);
if (initialPage) {
initialPage.el = f7View.current.router.currentPageEl;
const routeId = pages?.[0]?.props?.routeId;
if (routeId) initialPage.el.routeId = routeId;
if (initialRoute && initialRoute.route && initialRoute.route.keepAlive) {
initialRoute.route.keepAliveData = { pageEl: initialPage.el };
}
Expand Down
2 changes: 1 addition & 1 deletion src/react/shared/get-router-initial-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const getRouterInitialComponent = (router, initialComponent) => {
let initialComponentData;
const { initialUrl } = router.getInitialUrl();
const initialRoute = router.findMatchingRoute(initialUrl);
let routeProps = routeProps = router.propsHistory[router.propsHistory.length - 1];
let routeProps = router.propsHistory[router.propsHistory.length - 1];
// Store route props first, if not present, fallback to route options
if (!routeProps) {
if (initialRoute && initialRoute.route && initialRoute.route.options) {
Expand Down
4 changes: 4 additions & 0 deletions src/svelte/components/view.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
f7View.init(el);
if (initialPage) {
initialPage.el = f7View.router.currentPageEl;
const routeId = pages?.[0]?.props?.routeId;
if (routeId) initialPage.el.routeId = routeId;
if (initialRoute && initialRoute.route && initialRoute.route.keepAlive) {
initialRoute.route.keepAliveData = { pageEl: initialPage.el };
}
Expand All @@ -142,6 +144,8 @@
f7View.init(el);
if (initialPage) {
initialPage.el = f7View.router.currentPageEl;
const routeId = pages?.[0]?.props?.routeId;
if (routeId) initialPage.el.routeId = routeId;
if (initialRoute && initialRoute.route && initialRoute.route.keepAlive) {
initialRoute.route.keepAliveData = { pageEl: initialPage.el };
}
Expand Down
2 changes: 1 addition & 1 deletion src/svelte/shared/get-router-initial-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const getRouterInitialComponent = (router, initialComponent) => {
let initialComponentData;
const { initialUrl } = router.getInitialUrl();
const initialRoute = router.findMatchingRoute(initialUrl);
let routeProps = routeProps = router.propsHistory[router.propsHistory.length - 1];
let routeProps = router.propsHistory[router.propsHistory.length - 1];
// Store route props first, if not present, fallback to route options
if (!routeProps) {
if (initialRoute && initialRoute.route && initialRoute.route.options) {
Expand Down
4 changes: 4 additions & 0 deletions src/vue/components/view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ export default {
f7View.init(elRef.value);
if (initialPage) {
initialPage.el = f7View.router.currentPageEl;
const routeId = pages?.value[0]?.props?.routeId;
if (routeId) initialPage.el.routeId = routeId;
if (initialRoute && initialRoute.route && initialRoute.route.keepAlive) {
initialRoute.route.keepAliveData = { pageEl: initialPage.el };
}
Expand All @@ -285,6 +287,8 @@ export default {
f7View.init(elRef.value);
if (initialPage) {
initialPage.el = f7View.router.currentPageEl;
const routeId = pages?.value[0]?.props?.routeId;
if (routeId) initialPage.el.routeId = routeId;
if (initialRoute && initialRoute.route && initialRoute.route.keepAlive) {
initialRoute.route.keepAliveData = { pageEl: initialPage.el };
}
Expand Down
2 changes: 1 addition & 1 deletion src/vue/shared/get-router-initial-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const getRouterInitialComponent = (router, initialComponent) => {
let initialComponentData;
const { initialUrl } = router.getInitialUrl();
const initialRoute = router.findMatchingRoute(initialUrl);
let routeProps = routeProps = router.propsHistory[router.propsHistory.length - 1];
let routeProps = router.propsHistory[router.propsHistory.length - 1];
// Store route props first, if not present, fallback to route options
if (!routeProps) {
if (initialRoute && initialRoute.route && initialRoute.route.options) {
Expand Down