Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve robots.txt from a javascript webpack project

I have been battling with this, but all in vain. I am taking over this Webpack project that needs to have a robots.txt file running from its directory level, but I cannot get its routing to work. I dropped it next to where index.html is served from, but I keep getting Error Cannot Get robots.txt when I try to load or access it.

Is there any documentations or examples showing how to serve robots.txt from a Webpack projects?

Thanks,

like image 640
Zeusox Avatar asked Oct 21 '25 12:10

Zeusox


1 Answers

Use CopyWebpackPlugin https://webpack.js.org/plugins/copy-webpack-plugin/

$ npm install copy-webpack-plugin --save-dev

In your webpack.config.js file ...

const CopyPlugin = require('copy-webpack-plugin');
...
module.exports = {
  plugins: [
    new CopyPlugin([
      { from: 'source', to: 'dest' },
      { from: 'other', to: 'public' },
      { from: 'src/robots.txt', to: 'robots.txt' }
    ]),
  ],
};
like image 152
Kevin Avatar answered Oct 24 '25 03:10

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!