首页 > 解决方案 > 我如何在反应应用程序中添加 sitemap.xml 并公开?

问题描述

我在文件夹中的文件中有一个 sitemap.xml,/public我想将其公开,以便 Google 可以从 www.my_domain.com/sitemap.xml 读取它。我怎么做?

编辑 我已经接管了这个项目,我不知道这种设置。这就是我现在所拥有的。

// Set up the router, wrapping all Routes in the App component
import App from 'containers/App';
import createRoutes, { createIndexRoute } from './routes';

const rootRoute = {
  path: '/(en)(pl)(de)(bg)(cz)(ee)(gr)(hr)(hu)(it)(lt)(lv)(ro)(si)(sk)',
  component: App,
  childRoutes: createRoutes(store),
  indexRoute: createIndexRoute(store),
};

const render = (translatedMessages) => {
  ReactDOM.render(
    <Provider store={store}>
      <LanguageProvider messages={translatedMessages}>
        <Router
          history={history}
          routes={rootRoute}
          render={applyRouterMiddleware(
            useScroll(
              (prevRouterProps, { location }) =>
                prevRouterProps && location.pathname !== prevRouterProps.location.pathname
            )
          )}
        />
      </LanguageProvider>
    </Provider>,
    document.getElementById('app')
  );
};

每条路线看起来像这样

###routes.js
export default function createRoutes(store) {
  // Create reusable async injectors using getAsyncInjectors factory
  const { injectReducer, injectSagas } = getAsyncInjectors(store);

  return [
    {
      path: '/home',
      name: 'home',
      getComponent(nextState, cb) {
        const importModules = Promise.all([
          System.import('containers/HomePage/reducer'),
          System.import('containers/HomePage/sagas'),
          System.import('containers/HomePage'),
        ]);

        const renderRoute = loadModule(cb);

        importModules.then(([reducer, sagas, component]) => {
          injectReducer('home', reducer.default);
          injectSagas(sagas.default);

          renderRoute(component);
        });

        importModules.catch(errorLoading);
      },
    }
  ]
}

我只想要一条路线/sitemap.xml

标签: reactjsxml-sitemap

解决方案


我通过将 xml 添加到我的服务器重写规则来解决。

它是</^((?!\.(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$).)*$/>

更新为</^((?!\.(css|gif|ico|jpg|js|png|txt|svg|woff|xml|ttf)$).)*$/>

当我访问 /sitemap.xml 时,我现在可以看到站点地图在它显示页面未找到路线之前的位置。


推荐阅读