@@ -64,7 +64,9 @@ const INITIAL_STATE = {
6464 currentlyScannedUrlIndexes : [ ] ,
6565 forceStandardMode : false ,
6666 scannableUrls : [ ] ,
67+ scanOnce : false ,
6768 status : '' ,
69+ scansCount : 0 ,
6870 urlIndexesPendingScan : [ ] ,
6971} ;
7072
@@ -89,6 +91,7 @@ const CONCURRENT_VALIDATION_REQUESTS_WAIT_MS = 500;
8991 * @param {Object } action Action to call.
9092 * @return {Object } New state.
9193 */
94+ //eslint-disable-next-line complexity
9295export function siteScanReducer ( state , action ) {
9396 // Bail out early if Site Scan is skipped, i.e. if there is no validation nonce provided meaning the current user
9497 // does not have capabilities for running AMP validation.
@@ -113,9 +116,16 @@ export function siteScanReducer( state, action ) {
113116 } ;
114117 }
115118 case ACTION_SCANNABLE_URLS_RECEIVE : {
119+ if ( ! action . scannableUrls || action . scannableUrls ?. length === 0 ) {
120+ return {
121+ ...state ,
122+ status : STATUS_COMPLETED ,
123+ } ;
124+ }
125+
116126 return {
117127 ...state ,
118- status : action . scannableUrls ?. length > 0 ? STATUS_READY : STATUS_COMPLETED ,
128+ status : state . scanOnce && state . scansCount > 0 ? STATUS_COMPLETED : STATUS_READY ,
119129 scannableUrls : action . scannableUrls ,
120130 } ;
121131 }
@@ -124,10 +134,18 @@ export function siteScanReducer( state, action ) {
124134 return state ;
125135 }
126136
137+ if ( state . scanOnce && state . scansCount > 0 ) {
138+ return {
139+ ...state ,
140+ status : STATUS_COMPLETED ,
141+ } ;
142+ }
143+
127144 return {
128145 ...state ,
129146 status : STATUS_IDLE ,
130147 currentlyScannedUrlIndexes : [ ] ,
148+ scansCount : state . scansCount + 1 ,
131149 urlIndexesPendingScan : state . scannableUrls . map ( ( url , index ) => index ) ,
132150 } ;
133151 }
@@ -201,8 +219,9 @@ export function siteScanReducer( state, action ) {
201219 * @param {?any } props.children Component children.
202220 * @param {boolean } props.fetchCachedValidationErrors Whether to fetch cached validation errors on mount.
203221 * @param {boolean } props.refetchPluginSuppressionOnScanComplete Whether to refetch plugin suppression data when site scan is complete.
204- * @param {boolean } props.resetOnOptionsChange Whether to reset scanner and refetch scannable URLs whenever AMPoptions are changed.
222+ * @param {boolean } props.resetOnOptionsChange Whether to reset scanner and refetch scannable URLs whenever AMP options are changed.
205223 * @param {string } props.scannableUrlsRestPath The REST path for interacting with the scannable URL resources.
224+ * @param {boolean } props.scanOnce Whether to scan only once.
206225 * @param {string } props.validateNonce The AMP validate nonce.
207226 */
208227export function SiteScanContextProvider ( {
@@ -211,6 +230,7 @@ export function SiteScanContextProvider( {
211230 refetchPluginSuppressionOnScanComplete = false ,
212231 resetOnOptionsChange = false ,
213232 scannableUrlsRestPath,
233+ scanOnce = false ,
214234 validateNonce,
215235} ) {
216236 const {
@@ -221,7 +241,7 @@ export function SiteScanContextProvider( {
221241 refetchPluginSuppression,
222242 } = useContext ( Options ) ;
223243 const { setAsyncError } = useAsyncError ( ) ;
224- const [ state , dispatch ] = useReducer ( siteScanReducer , INITIAL_STATE ) ;
244+ const [ state , dispatch ] = useReducer ( siteScanReducer , { ... INITIAL_STATE , scanOnce } ) ;
225245 const {
226246 currentlyScannedUrlIndexes,
227247 forceStandardMode,
@@ -523,5 +543,6 @@ SiteScanContextProvider.propTypes = {
523543 refetchPluginSuppressionOnScanComplete : PropTypes . bool ,
524544 resetOnOptionsChange : PropTypes . bool ,
525545 scannableUrlsRestPath : PropTypes . string ,
546+ scanOnce : PropTypes . bool ,
526547 validateNonce : PropTypes . string ,
527548} ;
0 commit comments