reactjs - 如何在 React 应用程序中添加 sitemap.xml 并将其公开?

标签 reactjs xml-sitemap

我在 /public 文件夹中的文件中有一个 sitemap.xml,我想将其公开,以便 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 的路径

最佳答案

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

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

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

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

关于reactjs - 如何在 React 应用程序中添加 sitemap.xml 并将其公开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694909/

相关文章:

javascript - 你如何检查 react 测试库中的复选框?

wordpress - 站点地图生成器只检测主页,尖叫 Frog 也只检测主页

magento - Google sitemap.xml 和重复内容问题?

reactjs - 在 react 中提交表单后重定向到网站

javascript - 如何编辑开发和生产环境变量?

javascript - 我如何从 native react 中的辅助函数分派(dispatch)一个 Action

r - 如何使用 R 和 {XML} 包创建 sitemap.xml 文件?

apache - robots.txt 和 htaccess(而 CMS 位于子文件夹中)

php - 使用 PHP 创建 XML 站点地图

javascript - 在数组中推送许多对象会删除数组中所有先前的对象(使用 Hooks 进行 react )