reactjs - react + webpack - pass POST data to build -
coming php background, used have index.php 2 things:
- serve webpage if no parameters set;
- or serve json data when specific post parameter included in request.
something this:
// -- index.php <?php if ($_post["some_parameter"]) { ... echo json_encode(somearraydata); exit(0); } ?> <html> ... </html>
i have built complete frontend application npm, webpack, webpack-dev-server, , react. having completed first part, how can serve json data instead of html when request includes specific post parameter?
i can see 2 ways of doing this:
- build frontend usual , everytime build bundle, modify
index.html
, inject php code in it, , rename index.php. have run folder via apache or nginx, i'd able run index.php script. method downright ugly , worst way it. - run separate php server serves data or redirects static webpack-generated build. requests should start server, , server determines whether serve data or redirect frontend. problem comes neatly passing post data received request static react app. far know, way include url (get) parameter redirect , manually parse javascript on frontend. dirty solution, in opinion.
so, summarize:
- i need efficient way post data in react/webpack/webpack-dev-server environment.
- it should work hot-module-replacement dev setup.
- i'm fine switching node-based backend express.
- there shouldn't ajax involved in static react app.
any ideas? there has way properly.
update: solved copying index.php source directory build directory via webpack config. serve build folder php server , keep webpack --watch
building source.
i lose built-in features auto-reload , css injection, it's worth convenience of not having implement ssr simple task (getting single post variable).
for interested, added 2 npm scripts:
npm run start
runs original webpack-dev-server hot-reload, serving static content including static index.html filenpm run static
runswebpack --watch
copies index.php file build directory
this lets me have hot-reloading when developing frontend, , allows post data fetching when programming logic.
it's easy, convenient, , works on web hosting providers.
Comments
Post a Comment